aoutx.h 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651
  1. /* BFD semi-generic back-end for a.out binaries.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /*
  18. SECTION
  19. a.out backends
  20. DESCRIPTION
  21. BFD supports a number of different flavours of a.out format,
  22. though the major differences are only the sizes of the
  23. structures on disk, and the shape of the relocation
  24. information.
  25. The support is split into a basic support file @file{aoutx.h}
  26. and other files which derive functions from the base. One
  27. derivation file is @file{aoutf1.h} (for a.out flavour 1), and
  28. adds to the basic a.out functions support for sun3, sun4, 386
  29. and 29k a.out files, to create a target jump vector for a
  30. specific target.
  31. This information is further split out into more specific files
  32. for each machine, including @file{sunos.c} for sun3 and sun4,
  33. @file{newsos3.c} for the Sony NEWS, and @file{demo64.c} for a
  34. demonstration of a 64 bit a.out format.
  35. The base file @file{aoutx.h} defines general mechanisms for
  36. reading and writing records to and from disk and various
  37. other methods which BFD requires. It is included by
  38. @file{aout32.c} and @file{aout64.c} to form the names
  39. <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc.
  40. As an example, this is what goes on to make the back end for a
  41. sun4, from @file{aout32.c}:
  42. | #define ARCH_SIZE 32
  43. | #include "aoutx.h"
  44. Which exports names:
  45. | ...
  46. | aout_32_canonicalize_reloc
  47. | aout_32_find_nearest_line
  48. | aout_32_get_lineno
  49. | aout_32_get_reloc_upper_bound
  50. | ...
  51. from @file{sunos.c}:
  52. | #define TARGET_NAME "a.out-sunos-big"
  53. | #define VECNAME sparc_aout_sunos_be_vec
  54. | #include "aoutf1.h"
  55. requires all the names from @file{aout32.c}, and produces the jump vector
  56. | sparc_aout_sunos_be_vec
  57. The file @file{host-aout.c} is a special case. It is for a large set
  58. of hosts that use ``more or less standard'' a.out files, and
  59. for which cross-debugging is not interesting. It uses the
  60. standard 32-bit a.out support routines, but determines the
  61. file offsets and addresses of the text, data, and BSS
  62. sections, the machine architecture and machine type, and the
  63. entry point address, in a host-dependent manner. Once these
  64. values have been determined, generic code is used to handle
  65. the object file.
  66. When porting it to run on a new system, you must supply:
  67. | HOST_PAGE_SIZE
  68. | HOST_SEGMENT_SIZE
  69. | HOST_MACHINE_ARCH (optional)
  70. | HOST_MACHINE_MACHINE (optional)
  71. | HOST_TEXT_START_ADDR
  72. | HOST_STACK_END_ADDR
  73. in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These
  74. values, plus the structures and macros defined in @file{a.out.h} on
  75. your host system, will produce a BFD target that will access
  76. ordinary a.out files on your host. To configure a new machine
  77. to use @file{host-aout.c}, specify:
  78. | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
  79. | TDEPFILES= host-aout.o trad-core.o
  80. in the @file{config/@var{XXX}.mt} file, and modify @file{configure.ac}
  81. to use the
  82. @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
  83. configuration is selected. */
  84. /* Some assumptions:
  85. * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
  86. Doesn't matter what the setting of WP_TEXT is on output, but it'll
  87. get set on input.
  88. * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
  89. * Any BFD with both flags clear is OMAGIC.
  90. (Just want to make these explicit, so the conditions tested in this
  91. file make sense if you're more familiar with a.out than with BFD.) */
  92. #define KEEPIT udata.i
  93. #include "sysdep.h"
  94. #include "bfd.h"
  95. #include "safe-ctype.h"
  96. #include "bfdlink.h"
  97. #include "libaout.h"
  98. #include "libbfd.h"
  99. #include "aout/aout64.h"
  100. #include "aout/stab_gnu.h"
  101. #include "aout/ar.h"
  102. /*
  103. SUBSECTION
  104. Relocations
  105. DESCRIPTION
  106. The file @file{aoutx.h} provides for both the @emph{standard}
  107. and @emph{extended} forms of a.out relocation records.
  108. The standard records contain only an
  109. address, a symbol index, and a type field. The extended records
  110. (used on 29ks and sparcs) also have a full integer for an
  111. addend. */
  112. #ifndef CTOR_TABLE_RELOC_HOWTO
  113. #define CTOR_TABLE_RELOC_IDX 2
  114. #define CTOR_TABLE_RELOC_HOWTO(BFD) \
  115. ((obj_reloc_entry_size (BFD) == RELOC_EXT_SIZE \
  116. ? howto_table_ext : howto_table_std) \
  117. + CTOR_TABLE_RELOC_IDX)
  118. #endif
  119. #ifndef MY_swap_std_reloc_in
  120. #define MY_swap_std_reloc_in NAME (aout, swap_std_reloc_in)
  121. #endif
  122. #ifndef MY_swap_ext_reloc_in
  123. #define MY_swap_ext_reloc_in NAME (aout, swap_ext_reloc_in)
  124. #endif
  125. #ifndef MY_swap_std_reloc_out
  126. #define MY_swap_std_reloc_out NAME (aout, swap_std_reloc_out)
  127. #endif
  128. #ifndef MY_swap_ext_reloc_out
  129. #define MY_swap_ext_reloc_out NAME (aout, swap_ext_reloc_out)
  130. #endif
  131. #ifndef MY_final_link_relocate
  132. #define MY_final_link_relocate _bfd_final_link_relocate
  133. #endif
  134. #ifndef MY_relocate_contents
  135. #define MY_relocate_contents _bfd_relocate_contents
  136. #endif
  137. #define howto_table_ext NAME (aout, ext_howto_table)
  138. #define howto_table_std NAME (aout, std_howto_table)
  139. reloc_howto_type howto_table_ext[] =
  140. {
  141. /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
  142. HOWTO (RELOC_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "8", FALSE, 0, 0x000000ff, FALSE),
  143. HOWTO (RELOC_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, 0, "16", FALSE, 0, 0x0000ffff, FALSE),
  144. HOWTO (RELOC_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "32", FALSE, 0, 0xffffffff, FALSE),
  145. HOWTO (RELOC_DISP8, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0, "DISP8", FALSE, 0, 0x000000ff, FALSE),
  146. HOWTO (RELOC_DISP16, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0, "DISP16", FALSE, 0, 0x0000ffff, FALSE),
  147. HOWTO (RELOC_DISP32, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0, "DISP32", FALSE, 0, 0xffffffff, FALSE),
  148. HOWTO (RELOC_WDISP30, 2, 2, 30, TRUE, 0, complain_overflow_signed, 0, "WDISP30", FALSE, 0, 0x3fffffff, FALSE),
  149. HOWTO (RELOC_WDISP22, 2, 2, 22, TRUE, 0, complain_overflow_signed, 0, "WDISP22", FALSE, 0, 0x003fffff, FALSE),
  150. HOWTO (RELOC_HI22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "HI22", FALSE, 0, 0x003fffff, FALSE),
  151. HOWTO (RELOC_22, 0, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "22", FALSE, 0, 0x003fffff, FALSE),
  152. HOWTO (RELOC_13, 0, 2, 13, FALSE, 0, complain_overflow_bitfield, 0, "13", FALSE, 0, 0x00001fff, FALSE),
  153. HOWTO (RELOC_LO10, 0, 2, 10, FALSE, 0, complain_overflow_dont, 0, "LO10", FALSE, 0, 0x000003ff, FALSE),
  154. HOWTO (RELOC_SFA_BASE,0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "SFA_BASE", FALSE, 0, 0xffffffff, FALSE),
  155. HOWTO (RELOC_SFA_OFF13,0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "SFA_OFF13", FALSE, 0, 0xffffffff, FALSE),
  156. HOWTO (RELOC_BASE10, 0, 2, 10, FALSE, 0, complain_overflow_dont, 0, "BASE10", FALSE, 0, 0x000003ff, FALSE),
  157. HOWTO (RELOC_BASE13, 0, 2, 13, FALSE, 0, complain_overflow_signed, 0, "BASE13", FALSE, 0, 0x00001fff, FALSE),
  158. HOWTO (RELOC_BASE22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "BASE22", FALSE, 0, 0x003fffff, FALSE),
  159. HOWTO (RELOC_PC10, 0, 2, 10, TRUE, 0, complain_overflow_dont, 0, "PC10", FALSE, 0, 0x000003ff, TRUE),
  160. HOWTO (RELOC_PC22, 10, 2, 22, TRUE, 0, complain_overflow_signed, 0, "PC22", FALSE, 0, 0x003fffff, TRUE),
  161. HOWTO (RELOC_JMP_TBL, 2, 2, 30, TRUE, 0, complain_overflow_signed, 0, "JMP_TBL", FALSE, 0, 0x3fffffff, FALSE),
  162. HOWTO (RELOC_SEGOFF16,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "SEGOFF16", FALSE, 0, 0x00000000, FALSE),
  163. HOWTO (RELOC_GLOB_DAT,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "GLOB_DAT", FALSE, 0, 0x00000000, FALSE),
  164. HOWTO (RELOC_JMP_SLOT,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "JMP_SLOT", FALSE, 0, 0x00000000, FALSE),
  165. HOWTO (RELOC_RELATIVE,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "RELATIVE", FALSE, 0, 0x00000000, FALSE),
  166. HOWTO (0, 0, 3, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE",FALSE, 0, 0x00000000, TRUE),
  167. HOWTO (0, 0, 3, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE",FALSE, 0, 0x00000000, TRUE),
  168. #define RELOC_SPARC_REV32 RELOC_WDISP19
  169. HOWTO (RELOC_SPARC_REV32, 0, 2, 32, FALSE, 0, complain_overflow_dont, 0,"R_SPARC_REV32",FALSE, 0, 0xffffffff, FALSE),
  170. };
  171. /* Convert standard reloc records to "arelent" format (incl byte swap). */
  172. reloc_howto_type howto_table_std[] =
  173. {
  174. /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
  175. HOWTO ( 0, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,0,"8", TRUE, 0x000000ff,0x000000ff, FALSE),
  176. HOWTO ( 1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
  177. HOWTO ( 2, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"32", TRUE, 0xffffffff,0xffffffff, FALSE),
  178. HOWTO ( 3, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,0,"64", TRUE, 0xdeaddead,0xdeaddead, FALSE),
  179. HOWTO ( 4, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0,"DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
  180. HOWTO ( 5, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0,"DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
  181. HOWTO ( 6, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0,"DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
  182. HOWTO ( 7, 0, 4, 64, TRUE, 0, complain_overflow_signed, 0,"DISP64", TRUE, 0xfeedface,0xfeedface, FALSE),
  183. HOWTO ( 8, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"GOT_REL", FALSE, 0,0x00000000, FALSE),
  184. HOWTO ( 9, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"BASE16", FALSE,0xffffffff,0xffffffff, FALSE),
  185. HOWTO (10, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"BASE32", FALSE,0xffffffff,0xffffffff, FALSE),
  186. EMPTY_HOWTO (-1),
  187. EMPTY_HOWTO (-1),
  188. EMPTY_HOWTO (-1),
  189. EMPTY_HOWTO (-1),
  190. EMPTY_HOWTO (-1),
  191. HOWTO (16, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"JMP_TABLE", FALSE, 0,0x00000000, FALSE),
  192. EMPTY_HOWTO (-1),
  193. EMPTY_HOWTO (-1),
  194. EMPTY_HOWTO (-1),
  195. EMPTY_HOWTO (-1),
  196. EMPTY_HOWTO (-1),
  197. EMPTY_HOWTO (-1),
  198. EMPTY_HOWTO (-1),
  199. EMPTY_HOWTO (-1),
  200. EMPTY_HOWTO (-1),
  201. EMPTY_HOWTO (-1),
  202. EMPTY_HOWTO (-1),
  203. EMPTY_HOWTO (-1),
  204. EMPTY_HOWTO (-1),
  205. EMPTY_HOWTO (-1),
  206. EMPTY_HOWTO (-1),
  207. HOWTO (32, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"RELATIVE", FALSE, 0,0x00000000, FALSE),
  208. EMPTY_HOWTO (-1),
  209. EMPTY_HOWTO (-1),
  210. EMPTY_HOWTO (-1),
  211. EMPTY_HOWTO (-1),
  212. EMPTY_HOWTO (-1),
  213. EMPTY_HOWTO (-1),
  214. EMPTY_HOWTO (-1),
  215. HOWTO (40, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"BASEREL", FALSE, 0,0x00000000, FALSE),
  216. };
  217. #define TABLE_SIZE(TABLE) (sizeof (TABLE) / sizeof (TABLE[0]))
  218. reloc_howto_type *
  219. NAME (aout, reloc_type_lookup) (bfd *abfd, bfd_reloc_code_real_type code)
  220. {
  221. #define EXT(i, j) case i: return & howto_table_ext [j]
  222. #define STD(i, j) case i: return & howto_table_std [j]
  223. int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
  224. if (code == BFD_RELOC_CTOR)
  225. switch (bfd_arch_bits_per_address (abfd))
  226. {
  227. case 32:
  228. code = BFD_RELOC_32;
  229. break;
  230. case 64:
  231. code = BFD_RELOC_64;
  232. break;
  233. }
  234. if (ext)
  235. switch (code)
  236. {
  237. EXT (BFD_RELOC_8, 0);
  238. EXT (BFD_RELOC_16, 1);
  239. EXT (BFD_RELOC_32, 2);
  240. EXT (BFD_RELOC_HI22, 8);
  241. EXT (BFD_RELOC_LO10, 11);
  242. EXT (BFD_RELOC_32_PCREL_S2, 6);
  243. EXT (BFD_RELOC_SPARC_WDISP22, 7);
  244. EXT (BFD_RELOC_SPARC13, 10);
  245. EXT (BFD_RELOC_SPARC_GOT10, 14);
  246. EXT (BFD_RELOC_SPARC_BASE13, 15);
  247. EXT (BFD_RELOC_SPARC_GOT13, 15);
  248. EXT (BFD_RELOC_SPARC_GOT22, 16);
  249. EXT (BFD_RELOC_SPARC_PC10, 17);
  250. EXT (BFD_RELOC_SPARC_PC22, 18);
  251. EXT (BFD_RELOC_SPARC_WPLT30, 19);
  252. EXT (BFD_RELOC_SPARC_REV32, 26);
  253. default:
  254. return NULL;
  255. }
  256. else
  257. /* std relocs. */
  258. switch (code)
  259. {
  260. STD (BFD_RELOC_8, 0);
  261. STD (BFD_RELOC_16, 1);
  262. STD (BFD_RELOC_32, 2);
  263. STD (BFD_RELOC_8_PCREL, 4);
  264. STD (BFD_RELOC_16_PCREL, 5);
  265. STD (BFD_RELOC_32_PCREL, 6);
  266. STD (BFD_RELOC_16_BASEREL, 9);
  267. STD (BFD_RELOC_32_BASEREL, 10);
  268. default:
  269. return NULL;
  270. }
  271. }
  272. reloc_howto_type *
  273. NAME (aout, reloc_name_lookup) (bfd *abfd, const char *r_name)
  274. {
  275. unsigned int i, size;
  276. reloc_howto_type *howto_table;
  277. if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
  278. {
  279. howto_table = howto_table_ext;
  280. size = sizeof (howto_table_ext) / sizeof (howto_table_ext[0]);
  281. }
  282. else
  283. {
  284. howto_table = howto_table_std;
  285. size = sizeof (howto_table_std) / sizeof (howto_table_std[0]);
  286. }
  287. for (i = 0; i < size; i++)
  288. if (howto_table[i].name != NULL
  289. && strcasecmp (howto_table[i].name, r_name) == 0)
  290. return &howto_table[i];
  291. return NULL;
  292. }
  293. /*
  294. SUBSECTION
  295. Internal entry points
  296. DESCRIPTION
  297. @file{aoutx.h} exports several routines for accessing the
  298. contents of an a.out file, which are gathered and exported in
  299. turn by various format specific files (eg sunos.c).
  300. */
  301. /*
  302. FUNCTION
  303. aout_@var{size}_swap_exec_header_in
  304. SYNOPSIS
  305. void aout_@var{size}_swap_exec_header_in,
  306. (bfd *abfd,
  307. struct external_exec *bytes,
  308. struct internal_exec *execp);
  309. DESCRIPTION
  310. Swap the information in an executable header @var{raw_bytes} taken
  311. from a raw byte stream memory image into the internal exec header
  312. structure @var{execp}.
  313. */
  314. #ifndef NAME_swap_exec_header_in
  315. void
  316. NAME (aout, swap_exec_header_in) (bfd *abfd,
  317. struct external_exec *bytes,
  318. struct internal_exec *execp)
  319. {
  320. /* The internal_exec structure has some fields that are unused in this
  321. configuration (IE for i960), so ensure that all such uninitialized
  322. fields are zero'd out. There are places where two of these structs
  323. are memcmp'd, and thus the contents do matter. */
  324. memset ((void *) execp, 0, sizeof (struct internal_exec));
  325. /* Now fill in fields in the execp, from the bytes in the raw data. */
  326. execp->a_info = H_GET_32 (abfd, bytes->e_info);
  327. execp->a_text = GET_WORD (abfd, bytes->e_text);
  328. execp->a_data = GET_WORD (abfd, bytes->e_data);
  329. execp->a_bss = GET_WORD (abfd, bytes->e_bss);
  330. execp->a_syms = GET_WORD (abfd, bytes->e_syms);
  331. execp->a_entry = GET_WORD (abfd, bytes->e_entry);
  332. execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
  333. execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
  334. }
  335. #define NAME_swap_exec_header_in NAME (aout, swap_exec_header_in)
  336. #endif
  337. /*
  338. FUNCTION
  339. aout_@var{size}_swap_exec_header_out
  340. SYNOPSIS
  341. void aout_@var{size}_swap_exec_header_out
  342. (bfd *abfd,
  343. struct internal_exec *execp,
  344. struct external_exec *raw_bytes);
  345. DESCRIPTION
  346. Swap the information in an internal exec header structure
  347. @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
  348. */
  349. void
  350. NAME (aout, swap_exec_header_out) (bfd *abfd,
  351. struct internal_exec *execp,
  352. struct external_exec *bytes)
  353. {
  354. /* Now fill in fields in the raw data, from the fields in the exec struct. */
  355. H_PUT_32 (abfd, execp->a_info , bytes->e_info);
  356. PUT_WORD (abfd, execp->a_text , bytes->e_text);
  357. PUT_WORD (abfd, execp->a_data , bytes->e_data);
  358. PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
  359. PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
  360. PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
  361. PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
  362. PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
  363. }
  364. /* Make all the section for an a.out file. */
  365. bfd_boolean
  366. NAME (aout, make_sections) (bfd *abfd)
  367. {
  368. if (obj_textsec (abfd) == NULL && bfd_make_section (abfd, ".text") == NULL)
  369. return FALSE;
  370. if (obj_datasec (abfd) == NULL && bfd_make_section (abfd, ".data") == NULL)
  371. return FALSE;
  372. if (obj_bsssec (abfd) == NULL && bfd_make_section (abfd, ".bss") == NULL)
  373. return FALSE;
  374. return TRUE;
  375. }
  376. /*
  377. FUNCTION
  378. aout_@var{size}_some_aout_object_p
  379. SYNOPSIS
  380. const bfd_target *aout_@var{size}_some_aout_object_p
  381. (bfd *abfd,
  382. struct internal_exec *execp,
  383. const bfd_target *(*callback_to_real_object_p) (bfd *));
  384. DESCRIPTION
  385. Some a.out variant thinks that the file open in @var{abfd}
  386. checking is an a.out file. Do some more checking, and set up
  387. for access if it really is. Call back to the calling
  388. environment's "finish up" function just before returning, to
  389. handle any last-minute setup.
  390. */
  391. const bfd_target *
  392. NAME (aout, some_aout_object_p) (bfd *abfd,
  393. struct internal_exec *execp,
  394. const bfd_target *(*callback_to_real_object_p) (bfd *))
  395. {
  396. struct aout_data_struct *rawptr, *oldrawptr;
  397. const bfd_target *result;
  398. bfd_size_type amt = sizeof (* rawptr);
  399. rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
  400. if (rawptr == NULL)
  401. return NULL;
  402. oldrawptr = abfd->tdata.aout_data;
  403. abfd->tdata.aout_data = rawptr;
  404. /* Copy the contents of the old tdata struct.
  405. In particular, we want the subformat, since for hpux it was set in
  406. hp300hpux.c:swap_exec_header_in and will be used in
  407. hp300hpux.c:callback. */
  408. if (oldrawptr != NULL)
  409. *abfd->tdata.aout_data = *oldrawptr;
  410. abfd->tdata.aout_data->a.hdr = &rawptr->e;
  411. /* Copy in the internal_exec struct. */
  412. *(abfd->tdata.aout_data->a.hdr) = *execp;
  413. execp = abfd->tdata.aout_data->a.hdr;
  414. /* Set the file flags. */
  415. abfd->flags = BFD_NO_FLAGS;
  416. if (execp->a_drsize || execp->a_trsize)
  417. abfd->flags |= HAS_RELOC;
  418. /* Setting of EXEC_P has been deferred to the bottom of this function. */
  419. if (execp->a_syms)
  420. abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
  421. if (N_DYNAMIC (*execp))
  422. abfd->flags |= DYNAMIC;
  423. if (N_MAGIC (*execp) == ZMAGIC)
  424. {
  425. abfd->flags |= D_PAGED | WP_TEXT;
  426. adata (abfd).magic = z_magic;
  427. }
  428. else if (N_MAGIC (*execp) == QMAGIC)
  429. {
  430. abfd->flags |= D_PAGED | WP_TEXT;
  431. adata (abfd).magic = z_magic;
  432. adata (abfd).subformat = q_magic_format;
  433. }
  434. else if (N_MAGIC (*execp) == NMAGIC)
  435. {
  436. abfd->flags |= WP_TEXT;
  437. adata (abfd).magic = n_magic;
  438. }
  439. else if (N_MAGIC (*execp) == OMAGIC
  440. || N_MAGIC (*execp) == BMAGIC)
  441. adata (abfd).magic = o_magic;
  442. else
  443. /* Should have been checked with N_BADMAG before this routine
  444. was called. */
  445. abort ();
  446. bfd_get_start_address (abfd) = execp->a_entry;
  447. obj_aout_symbols (abfd) = NULL;
  448. bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
  449. /* The default relocation entry size is that of traditional V7 Unix. */
  450. obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  451. /* The default symbol entry size is that of traditional Unix. */
  452. obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
  453. #ifdef USE_MMAP
  454. bfd_init_window (&obj_aout_sym_window (abfd));
  455. bfd_init_window (&obj_aout_string_window (abfd));
  456. #endif
  457. obj_aout_external_syms (abfd) = NULL;
  458. obj_aout_external_strings (abfd) = NULL;
  459. obj_aout_sym_hashes (abfd) = NULL;
  460. if (! NAME (aout, make_sections) (abfd))
  461. goto error_ret;
  462. obj_datasec (abfd)->size = execp->a_data;
  463. obj_bsssec (abfd)->size = execp->a_bss;
  464. obj_textsec (abfd)->flags =
  465. (execp->a_trsize != 0
  466. ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
  467. : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
  468. obj_datasec (abfd)->flags =
  469. (execp->a_drsize != 0
  470. ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
  471. : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
  472. obj_bsssec (abfd)->flags = SEC_ALLOC;
  473. #ifdef THIS_IS_ONLY_DOCUMENTATION
  474. /* The common code can't fill in these things because they depend
  475. on either the start address of the text segment, the rounding
  476. up of virtual addresses between segments, or the starting file
  477. position of the text segment -- all of which varies among different
  478. versions of a.out. */
  479. /* Call back to the format-dependent code to fill in the rest of the
  480. fields and do any further cleanup. Things that should be filled
  481. in by the callback: */
  482. struct exec *execp = exec_hdr (abfd);
  483. obj_textsec (abfd)->size = N_TXTSIZE (*execp);
  484. /* Data and bss are already filled in since they're so standard. */
  485. /* The virtual memory addresses of the sections. */
  486. obj_textsec (abfd)->vma = N_TXTADDR (*execp);
  487. obj_datasec (abfd)->vma = N_DATADDR (*execp);
  488. obj_bsssec (abfd)->vma = N_BSSADDR (*execp);
  489. /* The file offsets of the sections. */
  490. obj_textsec (abfd)->filepos = N_TXTOFF (*execp);
  491. obj_datasec (abfd)->filepos = N_DATOFF (*execp);
  492. /* The file offsets of the relocation info. */
  493. obj_textsec (abfd)->rel_filepos = N_TRELOFF (*execp);
  494. obj_datasec (abfd)->rel_filepos = N_DRELOFF (*execp);
  495. /* The file offsets of the string table and symbol table. */
  496. obj_str_filepos (abfd) = N_STROFF (*execp);
  497. obj_sym_filepos (abfd) = N_SYMOFF (*execp);
  498. /* Determine the architecture and machine type of the object file. */
  499. switch (N_MACHTYPE (*exec_hdr (abfd)))
  500. {
  501. default:
  502. abfd->obj_arch = bfd_arch_obscure;
  503. break;
  504. }
  505. adata (abfd)->page_size = TARGET_PAGE_SIZE;
  506. adata (abfd)->segment_size = SEGMENT_SIZE;
  507. adata (abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
  508. return abfd->xvec;
  509. /* The architecture is encoded in various ways in various a.out variants,
  510. or is not encoded at all in some of them. The relocation size depends
  511. on the architecture and the a.out variant. Finally, the return value
  512. is the bfd_target vector in use. If an error occurs, return zero and
  513. set bfd_error to the appropriate error code.
  514. Formats such as b.out, which have additional fields in the a.out
  515. header, should cope with them in this callback as well. */
  516. #endif /* DOCUMENTATION */
  517. result = (*callback_to_real_object_p) (abfd);
  518. /* Now that the segment addresses have been worked out, take a better
  519. guess at whether the file is executable. If the entry point
  520. is within the text segment, assume it is. (This makes files
  521. executable even if their entry point address is 0, as long as
  522. their text starts at zero.).
  523. This test had to be changed to deal with systems where the text segment
  524. runs at a different location than the default. The problem is that the
  525. entry address can appear to be outside the text segment, thus causing an
  526. erroneous conclusion that the file isn't executable.
  527. To fix this, we now accept any non-zero entry point as an indication of
  528. executability. This will work most of the time, since only the linker
  529. sets the entry point, and that is likely to be non-zero for most systems. */
  530. if (execp->a_entry != 0
  531. || (execp->a_entry >= obj_textsec (abfd)->vma
  532. && execp->a_entry < (obj_textsec (abfd)->vma
  533. + obj_textsec (abfd)->size)
  534. && execp->a_trsize == 0
  535. && execp->a_drsize == 0))
  536. abfd->flags |= EXEC_P;
  537. #ifdef STAT_FOR_EXEC
  538. else
  539. {
  540. struct stat stat_buf;
  541. /* The original heuristic doesn't work in some important cases.
  542. The a.out file has no information about the text start
  543. address. For files (like kernels) linked to non-standard
  544. addresses (ld -Ttext nnn) the entry point may not be between
  545. the default text start (obj_textsec(abfd)->vma) and
  546. (obj_textsec(abfd)->vma) + text size. This is not just a mach
  547. issue. Many kernels are loaded at non standard addresses. */
  548. if (abfd->iostream != NULL
  549. && (abfd->flags & BFD_IN_MEMORY) == 0
  550. && (fstat (fileno ((FILE *) (abfd->iostream)), &stat_buf) == 0)
  551. && ((stat_buf.st_mode & 0111) != 0))
  552. abfd->flags |= EXEC_P;
  553. }
  554. #endif /* STAT_FOR_EXEC */
  555. if (result)
  556. return result;
  557. error_ret:
  558. bfd_release (abfd, rawptr);
  559. abfd->tdata.aout_data = oldrawptr;
  560. return NULL;
  561. }
  562. /*
  563. FUNCTION
  564. aout_@var{size}_mkobject
  565. SYNOPSIS
  566. bfd_boolean aout_@var{size}_mkobject, (bfd *abfd);
  567. DESCRIPTION
  568. Initialize BFD @var{abfd} for use with a.out files.
  569. */
  570. bfd_boolean
  571. NAME (aout, mkobject) (bfd *abfd)
  572. {
  573. struct aout_data_struct *rawptr;
  574. bfd_size_type amt = sizeof (* rawptr);
  575. bfd_set_error (bfd_error_system_call);
  576. rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
  577. if (rawptr == NULL)
  578. return FALSE;
  579. abfd->tdata.aout_data = rawptr;
  580. exec_hdr (abfd) = &(rawptr->e);
  581. obj_textsec (abfd) = NULL;
  582. obj_datasec (abfd) = NULL;
  583. obj_bsssec (abfd) = NULL;
  584. return TRUE;
  585. }
  586. /*
  587. FUNCTION
  588. aout_@var{size}_machine_type
  589. SYNOPSIS
  590. enum machine_type aout_@var{size}_machine_type
  591. (enum bfd_architecture arch,
  592. unsigned long machine,
  593. bfd_boolean *unknown);
  594. DESCRIPTION
  595. Keep track of machine architecture and machine type for
  596. a.out's. Return the <<machine_type>> for a particular
  597. architecture and machine, or <<M_UNKNOWN>> if that exact architecture
  598. and machine can't be represented in a.out format.
  599. If the architecture is understood, machine type 0 (default)
  600. is always understood.
  601. */
  602. enum machine_type
  603. NAME (aout, machine_type) (enum bfd_architecture arch,
  604. unsigned long machine,
  605. bfd_boolean *unknown)
  606. {
  607. enum machine_type arch_flags;
  608. arch_flags = M_UNKNOWN;
  609. *unknown = TRUE;
  610. switch (arch)
  611. {
  612. case bfd_arch_sparc:
  613. if (machine == 0
  614. || machine == bfd_mach_sparc
  615. || machine == bfd_mach_sparc_sparclite
  616. || machine == bfd_mach_sparc_sparclite_le
  617. || machine == bfd_mach_sparc_v8plus
  618. || machine == bfd_mach_sparc_v8plusa
  619. || machine == bfd_mach_sparc_v8plusb
  620. || machine == bfd_mach_sparc_v9
  621. || machine == bfd_mach_sparc_v9a
  622. || machine == bfd_mach_sparc_v9b)
  623. arch_flags = M_SPARC;
  624. else if (machine == bfd_mach_sparc_sparclet)
  625. arch_flags = M_SPARCLET;
  626. break;
  627. case bfd_arch_m68k:
  628. switch (machine)
  629. {
  630. case 0: arch_flags = M_68010; break;
  631. case bfd_mach_m68000: arch_flags = M_UNKNOWN; *unknown = FALSE; break;
  632. case bfd_mach_m68010: arch_flags = M_68010; break;
  633. case bfd_mach_m68020: arch_flags = M_68020; break;
  634. default: arch_flags = M_UNKNOWN; break;
  635. }
  636. break;
  637. case bfd_arch_i386:
  638. if (machine == 0
  639. || machine == bfd_mach_i386_i386
  640. || machine == bfd_mach_i386_i386_intel_syntax)
  641. arch_flags = M_386;
  642. break;
  643. case bfd_arch_arm:
  644. if (machine == 0)
  645. arch_flags = M_ARM;
  646. break;
  647. case bfd_arch_mips:
  648. switch (machine)
  649. {
  650. case 0:
  651. case bfd_mach_mips3000:
  652. case bfd_mach_mips3900:
  653. arch_flags = M_MIPS1;
  654. break;
  655. case bfd_mach_mips6000:
  656. arch_flags = M_MIPS2;
  657. break;
  658. case bfd_mach_mips4000:
  659. case bfd_mach_mips4010:
  660. case bfd_mach_mips4100:
  661. case bfd_mach_mips4300:
  662. case bfd_mach_mips4400:
  663. case bfd_mach_mips4600:
  664. case bfd_mach_mips4650:
  665. case bfd_mach_mips8000:
  666. case bfd_mach_mips9000:
  667. case bfd_mach_mips10000:
  668. case bfd_mach_mips12000:
  669. case bfd_mach_mips14000:
  670. case bfd_mach_mips16000:
  671. case bfd_mach_mips16:
  672. case bfd_mach_mipsisa32:
  673. case bfd_mach_mipsisa32r2:
  674. case bfd_mach_mipsisa32r3:
  675. case bfd_mach_mipsisa32r5:
  676. case bfd_mach_mipsisa32r6:
  677. case bfd_mach_mips5:
  678. case bfd_mach_mipsisa64:
  679. case bfd_mach_mipsisa64r2:
  680. case bfd_mach_mipsisa64r3:
  681. case bfd_mach_mipsisa64r5:
  682. case bfd_mach_mipsisa64r6:
  683. case bfd_mach_mips_sb1:
  684. case bfd_mach_mips_xlr:
  685. /* FIXME: These should be MIPS3, MIPS4, MIPS16, MIPS32, etc. */
  686. arch_flags = M_MIPS2;
  687. break;
  688. default:
  689. arch_flags = M_UNKNOWN;
  690. break;
  691. }
  692. break;
  693. case bfd_arch_ns32k:
  694. switch (machine)
  695. {
  696. case 0: arch_flags = M_NS32532; break;
  697. case 32032: arch_flags = M_NS32032; break;
  698. case 32532: arch_flags = M_NS32532; break;
  699. default: arch_flags = M_UNKNOWN; break;
  700. }
  701. break;
  702. case bfd_arch_vax:
  703. *unknown = FALSE;
  704. break;
  705. case bfd_arch_cris:
  706. if (machine == 0 || machine == 255)
  707. arch_flags = M_CRIS;
  708. break;
  709. case bfd_arch_m88k:
  710. *unknown = FALSE;
  711. break;
  712. default:
  713. arch_flags = M_UNKNOWN;
  714. }
  715. if (arch_flags != M_UNKNOWN)
  716. *unknown = FALSE;
  717. return arch_flags;
  718. }
  719. /*
  720. FUNCTION
  721. aout_@var{size}_set_arch_mach
  722. SYNOPSIS
  723. bfd_boolean aout_@var{size}_set_arch_mach,
  724. (bfd *,
  725. enum bfd_architecture arch,
  726. unsigned long machine);
  727. DESCRIPTION
  728. Set the architecture and the machine of the BFD @var{abfd} to the
  729. values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
  730. can support the architecture required.
  731. */
  732. bfd_boolean
  733. NAME (aout, set_arch_mach) (bfd *abfd,
  734. enum bfd_architecture arch,
  735. unsigned long machine)
  736. {
  737. if (! bfd_default_set_arch_mach (abfd, arch, machine))
  738. return FALSE;
  739. if (arch != bfd_arch_unknown)
  740. {
  741. bfd_boolean unknown;
  742. NAME (aout, machine_type) (arch, machine, &unknown);
  743. if (unknown)
  744. return FALSE;
  745. }
  746. /* Determine the size of a relocation entry. */
  747. switch (arch)
  748. {
  749. case bfd_arch_sparc:
  750. case bfd_arch_mips:
  751. obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
  752. break;
  753. default:
  754. obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  755. break;
  756. }
  757. return (*aout_backend_info (abfd)->set_sizes) (abfd);
  758. }
  759. static void
  760. adjust_o_magic (bfd *abfd, struct internal_exec *execp)
  761. {
  762. file_ptr pos = adata (abfd).exec_bytes_size;
  763. bfd_vma vma = 0;
  764. int pad = 0;
  765. /* Text. */
  766. obj_textsec (abfd)->filepos = pos;
  767. if (!obj_textsec (abfd)->user_set_vma)
  768. obj_textsec (abfd)->vma = vma;
  769. else
  770. vma = obj_textsec (abfd)->vma;
  771. pos += obj_textsec (abfd)->size;
  772. vma += obj_textsec (abfd)->size;
  773. /* Data. */
  774. if (!obj_datasec (abfd)->user_set_vma)
  775. {
  776. obj_textsec (abfd)->size += pad;
  777. pos += pad;
  778. vma += pad;
  779. obj_datasec (abfd)->vma = vma;
  780. }
  781. else
  782. vma = obj_datasec (abfd)->vma;
  783. obj_datasec (abfd)->filepos = pos;
  784. pos += obj_datasec (abfd)->size;
  785. vma += obj_datasec (abfd)->size;
  786. /* BSS. */
  787. if (!obj_bsssec (abfd)->user_set_vma)
  788. {
  789. obj_datasec (abfd)->size += pad;
  790. pos += pad;
  791. vma += pad;
  792. obj_bsssec (abfd)->vma = vma;
  793. }
  794. else
  795. {
  796. /* The VMA of the .bss section is set by the VMA of the
  797. .data section plus the size of the .data section. We may
  798. need to add padding bytes to make this true. */
  799. pad = obj_bsssec (abfd)->vma - vma;
  800. if (pad > 0)
  801. {
  802. obj_datasec (abfd)->size += pad;
  803. pos += pad;
  804. }
  805. }
  806. obj_bsssec (abfd)->filepos = pos;
  807. /* Fix up the exec header. */
  808. execp->a_text = obj_textsec (abfd)->size;
  809. execp->a_data = obj_datasec (abfd)->size;
  810. execp->a_bss = obj_bsssec (abfd)->size;
  811. N_SET_MAGIC (*execp, OMAGIC);
  812. }
  813. static void
  814. adjust_z_magic (bfd *abfd, struct internal_exec *execp)
  815. {
  816. bfd_size_type data_pad, text_pad;
  817. file_ptr text_end;
  818. const struct aout_backend_data *abdp;
  819. /* TRUE if text includes exec header. */
  820. bfd_boolean ztih;
  821. abdp = aout_backend_info (abfd);
  822. /* Text. */
  823. ztih = (abdp != NULL
  824. && (abdp->text_includes_header
  825. || obj_aout_subformat (abfd) == q_magic_format));
  826. obj_textsec (abfd)->filepos = (ztih
  827. ? adata (abfd).exec_bytes_size
  828. : adata (abfd).zmagic_disk_block_size);
  829. if (! obj_textsec (abfd)->user_set_vma)
  830. {
  831. /* ?? Do we really need to check for relocs here? */
  832. obj_textsec (abfd)->vma = ((abfd->flags & HAS_RELOC)
  833. ? 0
  834. : (ztih
  835. ? (abdp->default_text_vma
  836. + adata (abfd).exec_bytes_size)
  837. : abdp->default_text_vma));
  838. text_pad = 0;
  839. }
  840. else
  841. {
  842. /* The .text section is being loaded at an unusual address. We
  843. may need to pad it such that the .data section starts at a page
  844. boundary. */
  845. if (ztih)
  846. text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
  847. & (adata (abfd).page_size - 1));
  848. else
  849. text_pad = ((- obj_textsec (abfd)->vma)
  850. & (adata (abfd).page_size - 1));
  851. }
  852. /* Find start of data. */
  853. if (ztih)
  854. {
  855. text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->size;
  856. text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
  857. }
  858. else
  859. {
  860. /* Note that if page_size == zmagic_disk_block_size, then
  861. filepos == page_size, and this case is the same as the ztih
  862. case. */
  863. text_end = obj_textsec (abfd)->size;
  864. text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
  865. text_end += obj_textsec (abfd)->filepos;
  866. }
  867. obj_textsec (abfd)->size += text_pad;
  868. text_end += text_pad;
  869. /* Data. */
  870. if (!obj_datasec (abfd)->user_set_vma)
  871. {
  872. bfd_vma vma;
  873. vma = obj_textsec (abfd)->vma + obj_textsec (abfd)->size;
  874. obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
  875. }
  876. if (abdp && abdp->zmagic_mapped_contiguous)
  877. {
  878. asection * text = obj_textsec (abfd);
  879. asection * data = obj_datasec (abfd);
  880. text_pad = data->vma - (text->vma + text->size);
  881. /* Only pad the text section if the data
  882. section is going to be placed after it. */
  883. if (text_pad > 0)
  884. text->size += text_pad;
  885. }
  886. obj_datasec (abfd)->filepos = (obj_textsec (abfd)->filepos
  887. + obj_textsec (abfd)->size);
  888. /* Fix up exec header while we're at it. */
  889. execp->a_text = obj_textsec (abfd)->size;
  890. if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
  891. execp->a_text += adata (abfd).exec_bytes_size;
  892. if (obj_aout_subformat (abfd) == q_magic_format)
  893. N_SET_MAGIC (*execp, QMAGIC);
  894. else
  895. N_SET_MAGIC (*execp, ZMAGIC);
  896. /* Spec says data section should be rounded up to page boundary. */
  897. obj_datasec (abfd)->size
  898. = align_power (obj_datasec (abfd)->size,
  899. obj_bsssec (abfd)->alignment_power);
  900. execp->a_data = BFD_ALIGN (obj_datasec (abfd)->size,
  901. adata (abfd).page_size);
  902. data_pad = execp->a_data - obj_datasec (abfd)->size;
  903. /* BSS. */
  904. if (!obj_bsssec (abfd)->user_set_vma)
  905. obj_bsssec (abfd)->vma = (obj_datasec (abfd)->vma
  906. + obj_datasec (abfd)->size);
  907. /* If the BSS immediately follows the data section and extra space
  908. in the page is left after the data section, fudge data
  909. in the header so that the bss section looks smaller by that
  910. amount. We'll start the bss section there, and lie to the OS.
  911. (Note that a linker script, as well as the above assignment,
  912. could have explicitly set the BSS vma to immediately follow
  913. the data section.) */
  914. if (align_power (obj_bsssec (abfd)->vma, obj_bsssec (abfd)->alignment_power)
  915. == obj_datasec (abfd)->vma + obj_datasec (abfd)->size)
  916. execp->a_bss = (data_pad > obj_bsssec (abfd)->size
  917. ? 0 : obj_bsssec (abfd)->size - data_pad);
  918. else
  919. execp->a_bss = obj_bsssec (abfd)->size;
  920. }
  921. static void
  922. adjust_n_magic (bfd *abfd, struct internal_exec *execp)
  923. {
  924. file_ptr pos = adata (abfd).exec_bytes_size;
  925. bfd_vma vma = 0;
  926. int pad;
  927. /* Text. */
  928. obj_textsec (abfd)->filepos = pos;
  929. if (!obj_textsec (abfd)->user_set_vma)
  930. obj_textsec (abfd)->vma = vma;
  931. else
  932. vma = obj_textsec (abfd)->vma;
  933. pos += obj_textsec (abfd)->size;
  934. vma += obj_textsec (abfd)->size;
  935. /* Data. */
  936. obj_datasec (abfd)->filepos = pos;
  937. if (!obj_datasec (abfd)->user_set_vma)
  938. obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
  939. vma = obj_datasec (abfd)->vma;
  940. /* Since BSS follows data immediately, see if it needs alignment. */
  941. vma += obj_datasec (abfd)->size;
  942. pad = align_power (vma, obj_bsssec (abfd)->alignment_power) - vma;
  943. obj_datasec (abfd)->size += pad;
  944. pos += obj_datasec (abfd)->size;
  945. /* BSS. */
  946. if (!obj_bsssec (abfd)->user_set_vma)
  947. obj_bsssec (abfd)->vma = vma;
  948. else
  949. vma = obj_bsssec (abfd)->vma;
  950. /* Fix up exec header. */
  951. execp->a_text = obj_textsec (abfd)->size;
  952. execp->a_data = obj_datasec (abfd)->size;
  953. execp->a_bss = obj_bsssec (abfd)->size;
  954. N_SET_MAGIC (*execp, NMAGIC);
  955. }
  956. bfd_boolean
  957. NAME (aout, adjust_sizes_and_vmas) (bfd *abfd,
  958. bfd_size_type *text_size,
  959. file_ptr *text_end ATTRIBUTE_UNUSED)
  960. {
  961. struct internal_exec *execp = exec_hdr (abfd);
  962. if (! NAME (aout, make_sections) (abfd))
  963. return FALSE;
  964. if (adata (abfd).magic != undecided_magic)
  965. return TRUE;
  966. obj_textsec (abfd)->size =
  967. align_power (obj_textsec (abfd)->size,
  968. obj_textsec (abfd)->alignment_power);
  969. *text_size = obj_textsec (abfd)->size;
  970. /* Rule (heuristic) for when to pad to a new page. Note that there
  971. are (at least) two ways demand-paged (ZMAGIC) files have been
  972. handled. Most Berkeley-based systems start the text segment at
  973. (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
  974. segment right after the exec header; the latter is counted in the
  975. text segment size, and is paged in by the kernel with the rest of
  976. the text. */
  977. /* This perhaps isn't the right way to do this, but made it simpler for me
  978. to understand enough to implement it. Better would probably be to go
  979. right from BFD flags to alignment/positioning characteristics. But the
  980. old code was sloppy enough about handling the flags, and had enough
  981. other magic, that it was a little hard for me to understand. I think
  982. I understand it better now, but I haven't time to do the cleanup this
  983. minute. */
  984. if (abfd->flags & D_PAGED)
  985. /* Whether or not WP_TEXT is set -- let D_PAGED override. */
  986. adata (abfd).magic = z_magic;
  987. else if (abfd->flags & WP_TEXT)
  988. adata (abfd).magic = n_magic;
  989. else
  990. adata (abfd).magic = o_magic;
  991. #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
  992. #if __GNUC__ >= 2
  993. fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
  994. ({ char *str;
  995. switch (adata (abfd).magic)
  996. {
  997. case n_magic: str = "NMAGIC"; break;
  998. case o_magic: str = "OMAGIC"; break;
  999. case z_magic: str = "ZMAGIC"; break;
  1000. default: abort ();
  1001. }
  1002. str;
  1003. }),
  1004. obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
  1005. obj_textsec (abfd)->alignment_power,
  1006. obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
  1007. obj_datasec (abfd)->alignment_power,
  1008. obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size,
  1009. obj_bsssec (abfd)->alignment_power);
  1010. #endif
  1011. #endif
  1012. switch (adata (abfd).magic)
  1013. {
  1014. case o_magic:
  1015. adjust_o_magic (abfd, execp);
  1016. break;
  1017. case z_magic:
  1018. adjust_z_magic (abfd, execp);
  1019. break;
  1020. case n_magic:
  1021. adjust_n_magic (abfd, execp);
  1022. break;
  1023. default:
  1024. abort ();
  1025. }
  1026. #ifdef BFD_AOUT_DEBUG
  1027. fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
  1028. obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
  1029. obj_textsec (abfd)->filepos,
  1030. obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
  1031. obj_datasec (abfd)->filepos,
  1032. obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size);
  1033. #endif
  1034. return TRUE;
  1035. }
  1036. /*
  1037. FUNCTION
  1038. aout_@var{size}_new_section_hook
  1039. SYNOPSIS
  1040. bfd_boolean aout_@var{size}_new_section_hook,
  1041. (bfd *abfd,
  1042. asection *newsect);
  1043. DESCRIPTION
  1044. Called by the BFD in response to a @code{bfd_make_section}
  1045. request.
  1046. */
  1047. bfd_boolean
  1048. NAME (aout, new_section_hook) (bfd *abfd, asection *newsect)
  1049. {
  1050. /* Align to double at least. */
  1051. newsect->alignment_power = bfd_get_arch_info (abfd)->section_align_power;
  1052. if (bfd_get_format (abfd) == bfd_object)
  1053. {
  1054. if (obj_textsec (abfd) == NULL && !strcmp (newsect->name, ".text"))
  1055. {
  1056. obj_textsec (abfd)= newsect;
  1057. newsect->target_index = N_TEXT;
  1058. }
  1059. else if (obj_datasec (abfd) == NULL && !strcmp (newsect->name, ".data"))
  1060. {
  1061. obj_datasec (abfd) = newsect;
  1062. newsect->target_index = N_DATA;
  1063. }
  1064. else if (obj_bsssec (abfd) == NULL && !strcmp (newsect->name, ".bss"))
  1065. {
  1066. obj_bsssec (abfd) = newsect;
  1067. newsect->target_index = N_BSS;
  1068. }
  1069. }
  1070. /* We allow more than three sections internally. */
  1071. return _bfd_generic_new_section_hook (abfd, newsect);
  1072. }
  1073. bfd_boolean
  1074. NAME (aout, set_section_contents) (bfd *abfd,
  1075. sec_ptr section,
  1076. const void * location,
  1077. file_ptr offset,
  1078. bfd_size_type count)
  1079. {
  1080. file_ptr text_end;
  1081. bfd_size_type text_size;
  1082. if (! abfd->output_has_begun)
  1083. {
  1084. if (! NAME (aout, adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
  1085. return FALSE;
  1086. }
  1087. if (section == obj_bsssec (abfd))
  1088. {
  1089. bfd_set_error (bfd_error_no_contents);
  1090. return FALSE;
  1091. }
  1092. if (section != obj_textsec (abfd)
  1093. && section != obj_datasec (abfd))
  1094. {
  1095. if (aout_section_merge_with_text_p (abfd, section))
  1096. section->filepos = obj_textsec (abfd)->filepos +
  1097. (section->vma - obj_textsec (abfd)->vma);
  1098. else
  1099. {
  1100. (*_bfd_error_handler)
  1101. (_("%s: can not represent section `%s' in a.out object file format"),
  1102. bfd_get_filename (abfd), bfd_get_section_name (abfd, section));
  1103. bfd_set_error (bfd_error_nonrepresentable_section);
  1104. return FALSE;
  1105. }
  1106. }
  1107. if (count != 0)
  1108. {
  1109. if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
  1110. || bfd_bwrite (location, count, abfd) != count)
  1111. return FALSE;
  1112. }
  1113. return TRUE;
  1114. }
  1115. /* Read the external symbols from an a.out file. */
  1116. static bfd_boolean
  1117. aout_get_external_symbols (bfd *abfd)
  1118. {
  1119. if (obj_aout_external_syms (abfd) == NULL)
  1120. {
  1121. bfd_size_type count;
  1122. struct external_nlist *syms;
  1123. bfd_size_type amt = exec_hdr (abfd)->a_syms;
  1124. count = amt / EXTERNAL_NLIST_SIZE;
  1125. if (count == 0)
  1126. return TRUE; /* Nothing to do. */
  1127. #ifdef USE_MMAP
  1128. if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd), amt,
  1129. &obj_aout_sym_window (abfd), TRUE))
  1130. return FALSE;
  1131. syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
  1132. #else
  1133. /* We allocate using malloc to make the values easy to free
  1134. later on. If we put them on the objalloc it might not be
  1135. possible to free them. */
  1136. syms = (struct external_nlist *) bfd_malloc (amt);
  1137. if (syms == NULL)
  1138. return FALSE;
  1139. if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
  1140. || bfd_bread (syms, amt, abfd) != amt)
  1141. {
  1142. free (syms);
  1143. return FALSE;
  1144. }
  1145. #endif
  1146. obj_aout_external_syms (abfd) = syms;
  1147. obj_aout_external_sym_count (abfd) = count;
  1148. }
  1149. if (obj_aout_external_strings (abfd) == NULL
  1150. && exec_hdr (abfd)->a_syms != 0)
  1151. {
  1152. unsigned char string_chars[BYTES_IN_WORD];
  1153. bfd_size_type stringsize;
  1154. char *strings;
  1155. bfd_size_type amt = BYTES_IN_WORD;
  1156. /* Get the size of the strings. */
  1157. if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
  1158. || bfd_bread ((void *) string_chars, amt, abfd) != amt)
  1159. return FALSE;
  1160. stringsize = GET_WORD (abfd, string_chars);
  1161. #ifdef USE_MMAP
  1162. if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize,
  1163. &obj_aout_string_window (abfd), TRUE))
  1164. return FALSE;
  1165. strings = (char *) obj_aout_string_window (abfd).data;
  1166. #else
  1167. strings = (char *) bfd_malloc (stringsize + 1);
  1168. if (strings == NULL)
  1169. return FALSE;
  1170. /* Skip space for the string count in the buffer for convenience
  1171. when using indexes. */
  1172. amt = stringsize - BYTES_IN_WORD;
  1173. if (bfd_bread (strings + BYTES_IN_WORD, amt, abfd) != amt)
  1174. {
  1175. free (strings);
  1176. return FALSE;
  1177. }
  1178. #endif
  1179. /* Ensure that a zero index yields an empty string. */
  1180. strings[0] = '\0';
  1181. strings[stringsize - 1] = 0;
  1182. obj_aout_external_strings (abfd) = strings;
  1183. obj_aout_external_string_size (abfd) = stringsize;
  1184. }
  1185. return TRUE;
  1186. }
  1187. /* Translate an a.out symbol into a BFD symbol. The desc, other, type
  1188. and symbol->value fields of CACHE_PTR will be set from the a.out
  1189. nlist structure. This function is responsible for setting
  1190. symbol->flags and symbol->section, and adjusting symbol->value. */
  1191. static bfd_boolean
  1192. translate_from_native_sym_flags (bfd *abfd, aout_symbol_type *cache_ptr)
  1193. {
  1194. flagword visible;
  1195. if ((cache_ptr->type & N_STAB) != 0
  1196. || cache_ptr->type == N_FN)
  1197. {
  1198. asection *sec;
  1199. /* This is a debugging symbol. */
  1200. cache_ptr->symbol.flags = BSF_DEBUGGING;
  1201. /* Work out the symbol section. */
  1202. switch (cache_ptr->type & N_TYPE)
  1203. {
  1204. case N_TEXT:
  1205. case N_FN:
  1206. sec = obj_textsec (abfd);
  1207. break;
  1208. case N_DATA:
  1209. sec = obj_datasec (abfd);
  1210. break;
  1211. case N_BSS:
  1212. sec = obj_bsssec (abfd);
  1213. break;
  1214. default:
  1215. case N_ABS:
  1216. sec = bfd_abs_section_ptr;
  1217. break;
  1218. }
  1219. cache_ptr->symbol.section = sec;
  1220. cache_ptr->symbol.value -= sec->vma;
  1221. return TRUE;
  1222. }
  1223. /* Get the default visibility. This does not apply to all types, so
  1224. we just hold it in a local variable to use if wanted. */
  1225. if ((cache_ptr->type & N_EXT) == 0)
  1226. visible = BSF_LOCAL;
  1227. else
  1228. visible = BSF_GLOBAL;
  1229. switch (cache_ptr->type)
  1230. {
  1231. default:
  1232. case N_ABS: case N_ABS | N_EXT:
  1233. cache_ptr->symbol.section = bfd_abs_section_ptr;
  1234. cache_ptr->symbol.flags = visible;
  1235. break;
  1236. case N_UNDF | N_EXT:
  1237. if (cache_ptr->symbol.value != 0)
  1238. {
  1239. /* This is a common symbol. */
  1240. cache_ptr->symbol.flags = BSF_GLOBAL;
  1241. cache_ptr->symbol.section = bfd_com_section_ptr;
  1242. }
  1243. else
  1244. {
  1245. cache_ptr->symbol.flags = 0;
  1246. cache_ptr->symbol.section = bfd_und_section_ptr;
  1247. }
  1248. break;
  1249. case N_TEXT: case N_TEXT | N_EXT:
  1250. cache_ptr->symbol.section = obj_textsec (abfd);
  1251. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1252. cache_ptr->symbol.flags = visible;
  1253. break;
  1254. /* N_SETV symbols used to represent set vectors placed in the
  1255. data section. They are no longer generated. Theoretically,
  1256. it was possible to extract the entries and combine them with
  1257. new ones, although I don't know if that was ever actually
  1258. done. Unless that feature is restored, treat them as data
  1259. symbols. */
  1260. case N_SETV: case N_SETV | N_EXT:
  1261. case N_DATA: case N_DATA | N_EXT:
  1262. cache_ptr->symbol.section = obj_datasec (abfd);
  1263. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1264. cache_ptr->symbol.flags = visible;
  1265. break;
  1266. case N_BSS: case N_BSS | N_EXT:
  1267. cache_ptr->symbol.section = obj_bsssec (abfd);
  1268. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1269. cache_ptr->symbol.flags = visible;
  1270. break;
  1271. case N_SETA: case N_SETA | N_EXT:
  1272. case N_SETT: case N_SETT | N_EXT:
  1273. case N_SETD: case N_SETD | N_EXT:
  1274. case N_SETB: case N_SETB | N_EXT:
  1275. {
  1276. /* This code is no longer needed. It used to be used to make
  1277. the linker handle set symbols, but they are now handled in
  1278. the add_symbols routine instead. */
  1279. switch (cache_ptr->type & N_TYPE)
  1280. {
  1281. case N_SETA:
  1282. cache_ptr->symbol.section = bfd_abs_section_ptr;
  1283. break;
  1284. case N_SETT:
  1285. cache_ptr->symbol.section = obj_textsec (abfd);
  1286. break;
  1287. case N_SETD:
  1288. cache_ptr->symbol.section = obj_datasec (abfd);
  1289. break;
  1290. case N_SETB:
  1291. cache_ptr->symbol.section = obj_bsssec (abfd);
  1292. break;
  1293. }
  1294. cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
  1295. }
  1296. break;
  1297. case N_WARNING:
  1298. /* This symbol is the text of a warning message. The next
  1299. symbol is the symbol to associate the warning with. If a
  1300. reference is made to that symbol, a warning is issued. */
  1301. cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
  1302. cache_ptr->symbol.section = bfd_abs_section_ptr;
  1303. break;
  1304. case N_INDR: case N_INDR | N_EXT:
  1305. /* An indirect symbol. This consists of two symbols in a row.
  1306. The first symbol is the name of the indirection. The second
  1307. symbol is the name of the target. A reference to the first
  1308. symbol becomes a reference to the second. */
  1309. cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT | visible;
  1310. cache_ptr->symbol.section = bfd_ind_section_ptr;
  1311. break;
  1312. case N_WEAKU:
  1313. cache_ptr->symbol.section = bfd_und_section_ptr;
  1314. cache_ptr->symbol.flags = BSF_WEAK;
  1315. break;
  1316. case N_WEAKA:
  1317. cache_ptr->symbol.section = bfd_abs_section_ptr;
  1318. cache_ptr->symbol.flags = BSF_WEAK;
  1319. break;
  1320. case N_WEAKT:
  1321. cache_ptr->symbol.section = obj_textsec (abfd);
  1322. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1323. cache_ptr->symbol.flags = BSF_WEAK;
  1324. break;
  1325. case N_WEAKD:
  1326. cache_ptr->symbol.section = obj_datasec (abfd);
  1327. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1328. cache_ptr->symbol.flags = BSF_WEAK;
  1329. break;
  1330. case N_WEAKB:
  1331. cache_ptr->symbol.section = obj_bsssec (abfd);
  1332. cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1333. cache_ptr->symbol.flags = BSF_WEAK;
  1334. break;
  1335. }
  1336. return TRUE;
  1337. }
  1338. /* Set the fields of SYM_POINTER according to CACHE_PTR. */
  1339. static bfd_boolean
  1340. translate_to_native_sym_flags (bfd *abfd,
  1341. asymbol *cache_ptr,
  1342. struct external_nlist *sym_pointer)
  1343. {
  1344. bfd_vma value = cache_ptr->value;
  1345. asection *sec;
  1346. bfd_vma off;
  1347. /* Mask out any existing type bits in case copying from one section
  1348. to another. */
  1349. sym_pointer->e_type[0] &= ~N_TYPE;
  1350. sec = bfd_get_section (cache_ptr);
  1351. off = 0;
  1352. if (sec == NULL)
  1353. {
  1354. /* This case occurs, e.g., for the *DEBUG* section of a COFF
  1355. file. */
  1356. (*_bfd_error_handler)
  1357. (_("%s: can not represent section for symbol `%s' in a.out object file format"),
  1358. bfd_get_filename (abfd),
  1359. cache_ptr->name != NULL ? cache_ptr->name : _("*unknown*"));
  1360. bfd_set_error (bfd_error_nonrepresentable_section);
  1361. return FALSE;
  1362. }
  1363. if (sec->output_section != NULL)
  1364. {
  1365. off = sec->output_offset;
  1366. sec = sec->output_section;
  1367. }
  1368. if (bfd_is_abs_section (sec))
  1369. sym_pointer->e_type[0] |= N_ABS;
  1370. else if (sec == obj_textsec (abfd))
  1371. sym_pointer->e_type[0] |= N_TEXT;
  1372. else if (sec == obj_datasec (abfd))
  1373. sym_pointer->e_type[0] |= N_DATA;
  1374. else if (sec == obj_bsssec (abfd))
  1375. sym_pointer->e_type[0] |= N_BSS;
  1376. else if (bfd_is_und_section (sec))
  1377. sym_pointer->e_type[0] = N_UNDF | N_EXT;
  1378. else if (bfd_is_ind_section (sec))
  1379. sym_pointer->e_type[0] = N_INDR;
  1380. else if (bfd_is_com_section (sec))
  1381. sym_pointer->e_type[0] = N_UNDF | N_EXT;
  1382. else
  1383. {
  1384. if (aout_section_merge_with_text_p (abfd, sec))
  1385. sym_pointer->e_type[0] |= N_TEXT;
  1386. else
  1387. {
  1388. (*_bfd_error_handler)
  1389. (_("%s: can not represent section `%s' in a.out object file format"),
  1390. bfd_get_filename (abfd), bfd_get_section_name (abfd, sec));
  1391. bfd_set_error (bfd_error_nonrepresentable_section);
  1392. return FALSE;
  1393. }
  1394. }
  1395. /* Turn the symbol from section relative to absolute again. */
  1396. value += sec->vma + off;
  1397. if ((cache_ptr->flags & BSF_WARNING) != 0)
  1398. sym_pointer->e_type[0] = N_WARNING;
  1399. if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
  1400. sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
  1401. else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
  1402. sym_pointer->e_type[0] |= N_EXT;
  1403. else if ((cache_ptr->flags & BSF_LOCAL) != 0)
  1404. sym_pointer->e_type[0] &= ~N_EXT;
  1405. if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
  1406. {
  1407. int type = ((aout_symbol_type *) cache_ptr)->type;
  1408. switch (type)
  1409. {
  1410. case N_ABS: type = N_SETA; break;
  1411. case N_TEXT: type = N_SETT; break;
  1412. case N_DATA: type = N_SETD; break;
  1413. case N_BSS: type = N_SETB; break;
  1414. }
  1415. sym_pointer->e_type[0] = type;
  1416. }
  1417. if ((cache_ptr->flags & BSF_WEAK) != 0)
  1418. {
  1419. int type;
  1420. switch (sym_pointer->e_type[0] & N_TYPE)
  1421. {
  1422. default:
  1423. case N_ABS: type = N_WEAKA; break;
  1424. case N_TEXT: type = N_WEAKT; break;
  1425. case N_DATA: type = N_WEAKD; break;
  1426. case N_BSS: type = N_WEAKB; break;
  1427. case N_UNDF: type = N_WEAKU; break;
  1428. }
  1429. sym_pointer->e_type[0] = type;
  1430. }
  1431. PUT_WORD (abfd, value, sym_pointer->e_value);
  1432. return TRUE;
  1433. }
  1434. /* Native-level interface to symbols. */
  1435. asymbol *
  1436. NAME (aout, make_empty_symbol) (bfd *abfd)
  1437. {
  1438. bfd_size_type amt = sizeof (aout_symbol_type);
  1439. aout_symbol_type *new_symbol = (aout_symbol_type *) bfd_zalloc (abfd, amt);
  1440. if (!new_symbol)
  1441. return NULL;
  1442. new_symbol->symbol.the_bfd = abfd;
  1443. return &new_symbol->symbol;
  1444. }
  1445. /* Translate a set of internal symbols into external symbols. */
  1446. bfd_boolean
  1447. NAME (aout, translate_symbol_table) (bfd *abfd,
  1448. aout_symbol_type *in,
  1449. struct external_nlist *ext,
  1450. bfd_size_type count,
  1451. char *str,
  1452. bfd_size_type strsize,
  1453. bfd_boolean dynamic)
  1454. {
  1455. struct external_nlist *ext_end;
  1456. ext_end = ext + count;
  1457. for (; ext < ext_end; ext++, in++)
  1458. {
  1459. bfd_vma x;
  1460. x = GET_WORD (abfd, ext->e_strx);
  1461. in->symbol.the_bfd = abfd;
  1462. /* For the normal symbols, the zero index points at the number
  1463. of bytes in the string table but is to be interpreted as the
  1464. null string. For the dynamic symbols, the number of bytes in
  1465. the string table is stored in the __DYNAMIC structure and the
  1466. zero index points at an actual string. */
  1467. if (x == 0 && ! dynamic)
  1468. in->symbol.name = "";
  1469. else if (x < strsize)
  1470. in->symbol.name = str + x;
  1471. else
  1472. return FALSE;
  1473. in->symbol.value = GET_SWORD (abfd, ext->e_value);
  1474. in->desc = H_GET_16 (abfd, ext->e_desc);
  1475. in->other = H_GET_8 (abfd, ext->e_other);
  1476. in->type = H_GET_8 (abfd, ext->e_type);
  1477. in->symbol.udata.p = NULL;
  1478. if (! translate_from_native_sym_flags (abfd, in))
  1479. return FALSE;
  1480. if (dynamic)
  1481. in->symbol.flags |= BSF_DYNAMIC;
  1482. }
  1483. return TRUE;
  1484. }
  1485. /* We read the symbols into a buffer, which is discarded when this
  1486. function exits. We read the strings into a buffer large enough to
  1487. hold them all plus all the cached symbol entries. */
  1488. bfd_boolean
  1489. NAME (aout, slurp_symbol_table) (bfd *abfd)
  1490. {
  1491. struct external_nlist *old_external_syms;
  1492. aout_symbol_type *cached;
  1493. bfd_size_type cached_size;
  1494. /* If there's no work to be done, don't do any. */
  1495. if (obj_aout_symbols (abfd) != NULL)
  1496. return TRUE;
  1497. old_external_syms = obj_aout_external_syms (abfd);
  1498. if (! aout_get_external_symbols (abfd))
  1499. return FALSE;
  1500. cached_size = obj_aout_external_sym_count (abfd);
  1501. if (cached_size == 0)
  1502. return TRUE; /* Nothing to do. */
  1503. cached_size *= sizeof (aout_symbol_type);
  1504. cached = (aout_symbol_type *) bfd_zmalloc (cached_size);
  1505. if (cached == NULL)
  1506. return FALSE;
  1507. /* Convert from external symbol information to internal. */
  1508. if (! (NAME (aout, translate_symbol_table)
  1509. (abfd, cached,
  1510. obj_aout_external_syms (abfd),
  1511. obj_aout_external_sym_count (abfd),
  1512. obj_aout_external_strings (abfd),
  1513. obj_aout_external_string_size (abfd),
  1514. FALSE)))
  1515. {
  1516. free (cached);
  1517. return FALSE;
  1518. }
  1519. bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
  1520. obj_aout_symbols (abfd) = cached;
  1521. /* It is very likely that anybody who calls this function will not
  1522. want the external symbol information, so if it was allocated
  1523. because of our call to aout_get_external_symbols, we free it up
  1524. right away to save space. */
  1525. if (old_external_syms == NULL
  1526. && obj_aout_external_syms (abfd) != NULL)
  1527. {
  1528. #ifdef USE_MMAP
  1529. bfd_free_window (&obj_aout_sym_window (abfd));
  1530. #else
  1531. free (obj_aout_external_syms (abfd));
  1532. #endif
  1533. obj_aout_external_syms (abfd) = NULL;
  1534. }
  1535. return TRUE;
  1536. }
  1537. /* We use a hash table when writing out symbols so that we only write
  1538. out a particular string once. This helps particularly when the
  1539. linker writes out stabs debugging entries, because each different
  1540. contributing object file tends to have many duplicate stabs
  1541. strings.
  1542. This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
  1543. if BFD_TRADITIONAL_FORMAT is set. */
  1544. /* Get the index of a string in a strtab, adding it if it is not
  1545. already present. */
  1546. static inline bfd_size_type
  1547. add_to_stringtab (bfd *abfd,
  1548. struct bfd_strtab_hash *tab,
  1549. const char *str,
  1550. bfd_boolean copy)
  1551. {
  1552. bfd_boolean hash;
  1553. bfd_size_type str_index;
  1554. /* An index of 0 always means the empty string. */
  1555. if (str == 0 || *str == '\0')
  1556. return 0;
  1557. /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
  1558. doesn't understand a hashed string table. */
  1559. hash = TRUE;
  1560. if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
  1561. hash = FALSE;
  1562. str_index = _bfd_stringtab_add (tab, str, hash, copy);
  1563. if (str_index != (bfd_size_type) -1)
  1564. /* Add BYTES_IN_WORD to the return value to account for the
  1565. space taken up by the string table size. */
  1566. str_index += BYTES_IN_WORD;
  1567. return str_index;
  1568. }
  1569. /* Write out a strtab. ABFD is already at the right location in the
  1570. file. */
  1571. static bfd_boolean
  1572. emit_stringtab (bfd *abfd, struct bfd_strtab_hash *tab)
  1573. {
  1574. bfd_byte buffer[BYTES_IN_WORD];
  1575. bfd_size_type amt = BYTES_IN_WORD;
  1576. /* The string table starts with the size. */
  1577. PUT_WORD (abfd, _bfd_stringtab_size (tab) + BYTES_IN_WORD, buffer);
  1578. if (bfd_bwrite ((void *) buffer, amt, abfd) != amt)
  1579. return FALSE;
  1580. return _bfd_stringtab_emit (abfd, tab);
  1581. }
  1582. bfd_boolean
  1583. NAME (aout, write_syms) (bfd *abfd)
  1584. {
  1585. unsigned int count ;
  1586. asymbol **generic = bfd_get_outsymbols (abfd);
  1587. struct bfd_strtab_hash *strtab;
  1588. strtab = _bfd_stringtab_init ();
  1589. if (strtab == NULL)
  1590. return FALSE;
  1591. for (count = 0; count < bfd_get_symcount (abfd); count++)
  1592. {
  1593. asymbol *g = generic[count];
  1594. bfd_size_type indx;
  1595. struct external_nlist nsp;
  1596. bfd_size_type amt;
  1597. indx = add_to_stringtab (abfd, strtab, g->name, FALSE);
  1598. if (indx == (bfd_size_type) -1)
  1599. goto error_return;
  1600. PUT_WORD (abfd, indx, (bfd_byte *) nsp.e_strx);
  1601. if (bfd_asymbol_flavour (g) == abfd->xvec->flavour)
  1602. {
  1603. H_PUT_16 (abfd, aout_symbol (g)->desc, nsp.e_desc);
  1604. H_PUT_8 (abfd, aout_symbol (g)->other, nsp.e_other);
  1605. H_PUT_8 (abfd, aout_symbol (g)->type, nsp.e_type);
  1606. }
  1607. else
  1608. {
  1609. H_PUT_16 (abfd, 0, nsp.e_desc);
  1610. H_PUT_8 (abfd, 0, nsp.e_other);
  1611. H_PUT_8 (abfd, 0, nsp.e_type);
  1612. }
  1613. if (! translate_to_native_sym_flags (abfd, g, &nsp))
  1614. goto error_return;
  1615. amt = EXTERNAL_NLIST_SIZE;
  1616. if (bfd_bwrite ((void *) &nsp, amt, abfd) != amt)
  1617. goto error_return;
  1618. /* NB: `KEEPIT' currently overlays `udata.p', so set this only
  1619. here, at the end. */
  1620. g->KEEPIT = count;
  1621. }
  1622. if (! emit_stringtab (abfd, strtab))
  1623. goto error_return;
  1624. _bfd_stringtab_free (strtab);
  1625. return TRUE;
  1626. error_return:
  1627. _bfd_stringtab_free (strtab);
  1628. return FALSE;
  1629. }
  1630. long
  1631. NAME (aout, canonicalize_symtab) (bfd *abfd, asymbol **location)
  1632. {
  1633. unsigned int counter = 0;
  1634. aout_symbol_type *symbase;
  1635. if (!NAME (aout, slurp_symbol_table) (abfd))
  1636. return -1;
  1637. for (symbase = obj_aout_symbols (abfd);
  1638. counter++ < bfd_get_symcount (abfd);
  1639. )
  1640. *(location++) = (asymbol *) (symbase++);
  1641. *location++ =0;
  1642. return bfd_get_symcount (abfd);
  1643. }
  1644. /* Standard reloc stuff. */
  1645. /* Output standard relocation information to a file in target byte order. */
  1646. extern void NAME (aout, swap_std_reloc_out)
  1647. (bfd *, arelent *, struct reloc_std_external *);
  1648. void
  1649. NAME (aout, swap_std_reloc_out) (bfd *abfd,
  1650. arelent *g,
  1651. struct reloc_std_external *natptr)
  1652. {
  1653. int r_index;
  1654. asymbol *sym = *(g->sym_ptr_ptr);
  1655. int r_extern;
  1656. unsigned int r_length;
  1657. int r_pcrel;
  1658. int r_baserel, r_jmptable, r_relative;
  1659. asection *output_section = sym->section->output_section;
  1660. PUT_WORD (abfd, g->address, natptr->r_address);
  1661. r_length = g->howto->size ; /* Size as a power of two. */
  1662. r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
  1663. /* XXX This relies on relocs coming from a.out files. */
  1664. r_baserel = (g->howto->type & 8) != 0;
  1665. r_jmptable = (g->howto->type & 16) != 0;
  1666. r_relative = (g->howto->type & 32) != 0;
  1667. /* Name was clobbered by aout_write_syms to be symbol index. */
  1668. /* If this relocation is relative to a symbol then set the
  1669. r_index to the symbols index, and the r_extern bit.
  1670. Absolute symbols can come in in two ways, either as an offset
  1671. from the abs section, or as a symbol which has an abs value.
  1672. check for that here. */
  1673. if (bfd_is_com_section (output_section)
  1674. || bfd_is_abs_section (output_section)
  1675. || bfd_is_und_section (output_section)
  1676. /* PR gas/3041 a.out relocs against weak symbols
  1677. must be treated as if they were against externs. */
  1678. || (sym->flags & BSF_WEAK))
  1679. {
  1680. if (bfd_abs_section_ptr->symbol == sym)
  1681. {
  1682. /* Whoops, looked like an abs symbol, but is
  1683. really an offset from the abs section. */
  1684. r_index = N_ABS;
  1685. r_extern = 0;
  1686. }
  1687. else
  1688. {
  1689. /* Fill in symbol. */
  1690. r_extern = 1;
  1691. r_index = (*(g->sym_ptr_ptr))->KEEPIT;
  1692. }
  1693. }
  1694. else
  1695. {
  1696. /* Just an ordinary section. */
  1697. r_extern = 0;
  1698. r_index = output_section->target_index;
  1699. }
  1700. /* Now the fun stuff. */
  1701. if (bfd_header_big_endian (abfd))
  1702. {
  1703. natptr->r_index[0] = r_index >> 16;
  1704. natptr->r_index[1] = r_index >> 8;
  1705. natptr->r_index[2] = r_index;
  1706. natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
  1707. | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
  1708. | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
  1709. | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
  1710. | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
  1711. | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
  1712. }
  1713. else
  1714. {
  1715. natptr->r_index[2] = r_index >> 16;
  1716. natptr->r_index[1] = r_index >> 8;
  1717. natptr->r_index[0] = r_index;
  1718. natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
  1719. | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
  1720. | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
  1721. | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
  1722. | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
  1723. | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
  1724. }
  1725. }
  1726. /* Extended stuff. */
  1727. /* Output extended relocation information to a file in target byte order. */
  1728. extern void NAME (aout, swap_ext_reloc_out)
  1729. (bfd *, arelent *, struct reloc_ext_external *);
  1730. void
  1731. NAME (aout, swap_ext_reloc_out) (bfd *abfd,
  1732. arelent *g,
  1733. struct reloc_ext_external *natptr)
  1734. {
  1735. int r_index;
  1736. int r_extern;
  1737. unsigned int r_type;
  1738. bfd_vma r_addend;
  1739. asymbol *sym = *(g->sym_ptr_ptr);
  1740. asection *output_section = sym->section->output_section;
  1741. PUT_WORD (abfd, g->address, natptr->r_address);
  1742. r_type = (unsigned int) g->howto->type;
  1743. r_addend = g->addend;
  1744. if ((sym->flags & BSF_SECTION_SYM) != 0)
  1745. r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
  1746. /* If this relocation is relative to a symbol then set the
  1747. r_index to the symbols index, and the r_extern bit.
  1748. Absolute symbols can come in in two ways, either as an offset
  1749. from the abs section, or as a symbol which has an abs value.
  1750. check for that here. */
  1751. if (bfd_is_abs_section (bfd_get_section (sym)))
  1752. {
  1753. r_extern = 0;
  1754. r_index = N_ABS;
  1755. }
  1756. else if ((sym->flags & BSF_SECTION_SYM) == 0)
  1757. {
  1758. if (bfd_is_und_section (bfd_get_section (sym))
  1759. || (sym->flags & BSF_GLOBAL) != 0)
  1760. r_extern = 1;
  1761. else
  1762. r_extern = 0;
  1763. r_index = (*(g->sym_ptr_ptr))->KEEPIT;
  1764. }
  1765. else
  1766. {
  1767. /* Just an ordinary section. */
  1768. r_extern = 0;
  1769. r_index = output_section->target_index;
  1770. }
  1771. /* Now the fun stuff. */
  1772. if (bfd_header_big_endian (abfd))
  1773. {
  1774. natptr->r_index[0] = r_index >> 16;
  1775. natptr->r_index[1] = r_index >> 8;
  1776. natptr->r_index[2] = r_index;
  1777. natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
  1778. | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
  1779. }
  1780. else
  1781. {
  1782. natptr->r_index[2] = r_index >> 16;
  1783. natptr->r_index[1] = r_index >> 8;
  1784. natptr->r_index[0] = r_index;
  1785. natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
  1786. | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE));
  1787. }
  1788. PUT_WORD (abfd, r_addend, natptr->r_addend);
  1789. }
  1790. /* BFD deals internally with all things based from the section they're
  1791. in. so, something in 10 bytes into a text section with a base of
  1792. 50 would have a symbol (.text+10) and know .text vma was 50.
  1793. Aout keeps all it's symbols based from zero, so the symbol would
  1794. contain 60. This macro subs the base of each section from the value
  1795. to give the true offset from the section. */
  1796. #define MOVE_ADDRESS(ad) \
  1797. if (r_extern) \
  1798. { \
  1799. /* Undefined symbol. */ \
  1800. cache_ptr->sym_ptr_ptr = symbols + r_index; \
  1801. cache_ptr->addend = ad; \
  1802. } \
  1803. else \
  1804. { \
  1805. /* Defined, section relative. Replace symbol with pointer to \
  1806. symbol which points to section. */ \
  1807. switch (r_index) \
  1808. { \
  1809. case N_TEXT: \
  1810. case N_TEXT | N_EXT: \
  1811. cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
  1812. cache_ptr->addend = ad - su->textsec->vma; \
  1813. break; \
  1814. case N_DATA: \
  1815. case N_DATA | N_EXT: \
  1816. cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
  1817. cache_ptr->addend = ad - su->datasec->vma; \
  1818. break; \
  1819. case N_BSS: \
  1820. case N_BSS | N_EXT: \
  1821. cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
  1822. cache_ptr->addend = ad - su->bsssec->vma; \
  1823. break; \
  1824. default: \
  1825. case N_ABS: \
  1826. case N_ABS | N_EXT: \
  1827. cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
  1828. cache_ptr->addend = ad; \
  1829. break; \
  1830. } \
  1831. }
  1832. void
  1833. NAME (aout, swap_ext_reloc_in) (bfd *abfd,
  1834. struct reloc_ext_external *bytes,
  1835. arelent *cache_ptr,
  1836. asymbol **symbols,
  1837. bfd_size_type symcount)
  1838. {
  1839. unsigned int r_index;
  1840. int r_extern;
  1841. unsigned int r_type;
  1842. struct aoutdata *su = &(abfd->tdata.aout_data->a);
  1843. cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
  1844. /* Now the fun stuff. */
  1845. if (bfd_header_big_endian (abfd))
  1846. {
  1847. r_index = (((unsigned int) bytes->r_index[0] << 16)
  1848. | ((unsigned int) bytes->r_index[1] << 8)
  1849. | bytes->r_index[2]);
  1850. r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
  1851. r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
  1852. >> RELOC_EXT_BITS_TYPE_SH_BIG);
  1853. }
  1854. else
  1855. {
  1856. r_index = (((unsigned int) bytes->r_index[2] << 16)
  1857. | ((unsigned int) bytes->r_index[1] << 8)
  1858. | bytes->r_index[0]);
  1859. r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
  1860. r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
  1861. >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
  1862. }
  1863. if (r_type < TABLE_SIZE (howto_table_ext))
  1864. cache_ptr->howto = howto_table_ext + r_type;
  1865. else
  1866. cache_ptr->howto = NULL;
  1867. /* Base relative relocs are always against the symbol table,
  1868. regardless of the setting of r_extern. r_extern just reflects
  1869. whether the symbol the reloc is against is local or global. */
  1870. if (r_type == (unsigned int) RELOC_BASE10
  1871. || r_type == (unsigned int) RELOC_BASE13
  1872. || r_type == (unsigned int) RELOC_BASE22)
  1873. r_extern = 1;
  1874. if (r_extern && r_index > symcount)
  1875. {
  1876. /* We could arrange to return an error, but it might be useful
  1877. to see the file even if it is bad. */
  1878. r_extern = 0;
  1879. r_index = N_ABS;
  1880. }
  1881. MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
  1882. }
  1883. void
  1884. NAME (aout, swap_std_reloc_in) (bfd *abfd,
  1885. struct reloc_std_external *bytes,
  1886. arelent *cache_ptr,
  1887. asymbol **symbols,
  1888. bfd_size_type symcount)
  1889. {
  1890. unsigned int r_index;
  1891. int r_extern;
  1892. unsigned int r_length;
  1893. int r_pcrel;
  1894. int r_baserel, r_jmptable, r_relative;
  1895. struct aoutdata *su = &(abfd->tdata.aout_data->a);
  1896. unsigned int howto_idx;
  1897. cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
  1898. /* Now the fun stuff. */
  1899. if (bfd_header_big_endian (abfd))
  1900. {
  1901. r_index = (((unsigned int) bytes->r_index[0] << 16)
  1902. | ((unsigned int) bytes->r_index[1] << 8)
  1903. | bytes->r_index[2]);
  1904. r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
  1905. r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
  1906. r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
  1907. r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
  1908. r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
  1909. r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
  1910. >> RELOC_STD_BITS_LENGTH_SH_BIG);
  1911. }
  1912. else
  1913. {
  1914. r_index = (((unsigned int) bytes->r_index[2] << 16)
  1915. | ((unsigned int) bytes->r_index[1] << 8)
  1916. | bytes->r_index[0]);
  1917. r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
  1918. r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
  1919. r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
  1920. r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
  1921. r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
  1922. r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
  1923. >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
  1924. }
  1925. howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
  1926. + 16 * r_jmptable + 32 * r_relative);
  1927. if (howto_idx < TABLE_SIZE (howto_table_std))
  1928. {
  1929. cache_ptr->howto = howto_table_std + howto_idx;
  1930. if (cache_ptr->howto->type == (unsigned int) -1)
  1931. cache_ptr->howto = NULL;
  1932. }
  1933. else
  1934. cache_ptr->howto = NULL;
  1935. /* Base relative relocs are always against the symbol table,
  1936. regardless of the setting of r_extern. r_extern just reflects
  1937. whether the symbol the reloc is against is local or global. */
  1938. if (r_baserel)
  1939. r_extern = 1;
  1940. if (r_extern && r_index > symcount)
  1941. {
  1942. /* We could arrange to return an error, but it might be useful
  1943. to see the file even if it is bad. */
  1944. r_extern = 0;
  1945. r_index = N_ABS;
  1946. }
  1947. MOVE_ADDRESS (0);
  1948. }
  1949. /* Read and swap the relocs for a section. */
  1950. bfd_boolean
  1951. NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
  1952. {
  1953. bfd_size_type count;
  1954. bfd_size_type reloc_size;
  1955. void * relocs;
  1956. arelent *reloc_cache;
  1957. size_t each_size;
  1958. unsigned int counter = 0;
  1959. arelent *cache_ptr;
  1960. bfd_size_type amt;
  1961. if (asect->relocation)
  1962. return TRUE;
  1963. if (asect->flags & SEC_CONSTRUCTOR)
  1964. return TRUE;
  1965. if (asect == obj_datasec (abfd))
  1966. reloc_size = exec_hdr (abfd)->a_drsize;
  1967. else if (asect == obj_textsec (abfd))
  1968. reloc_size = exec_hdr (abfd)->a_trsize;
  1969. else if (asect == obj_bsssec (abfd))
  1970. reloc_size = 0;
  1971. else
  1972. {
  1973. bfd_set_error (bfd_error_invalid_operation);
  1974. return FALSE;
  1975. }
  1976. if (reloc_size == 0)
  1977. return TRUE; /* Nothing to be done. */
  1978. if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
  1979. return FALSE;
  1980. each_size = obj_reloc_entry_size (abfd);
  1981. count = reloc_size / each_size;
  1982. if (count == 0)
  1983. return TRUE; /* Nothing to be done. */
  1984. amt = count * sizeof (arelent);
  1985. reloc_cache = (arelent *) bfd_zmalloc (amt);
  1986. if (reloc_cache == NULL)
  1987. return FALSE;
  1988. relocs = bfd_malloc (reloc_size);
  1989. if (relocs == NULL)
  1990. {
  1991. free (reloc_cache);
  1992. return FALSE;
  1993. }
  1994. if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
  1995. {
  1996. free (relocs);
  1997. free (reloc_cache);
  1998. return FALSE;
  1999. }
  2000. cache_ptr = reloc_cache;
  2001. if (each_size == RELOC_EXT_SIZE)
  2002. {
  2003. struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;
  2004. for (; counter < count; counter++, rptr++, cache_ptr++)
  2005. MY_swap_ext_reloc_in (abfd, rptr, cache_ptr, symbols,
  2006. (bfd_size_type) bfd_get_symcount (abfd));
  2007. }
  2008. else
  2009. {
  2010. struct reloc_std_external *rptr = (struct reloc_std_external *) relocs;
  2011. for (; counter < count; counter++, rptr++, cache_ptr++)
  2012. MY_swap_std_reloc_in (abfd, rptr, cache_ptr, symbols,
  2013. (bfd_size_type) bfd_get_symcount (abfd));
  2014. }
  2015. free (relocs);
  2016. asect->relocation = reloc_cache;
  2017. asect->reloc_count = cache_ptr - reloc_cache;
  2018. return TRUE;
  2019. }
  2020. /* Write out a relocation section into an object file. */
  2021. bfd_boolean
  2022. NAME (aout, squirt_out_relocs) (bfd *abfd, asection *section)
  2023. {
  2024. arelent **generic;
  2025. unsigned char *native, *natptr;
  2026. size_t each_size;
  2027. unsigned int count = section->reloc_count;
  2028. bfd_size_type natsize;
  2029. if (count == 0 || section->orelocation == NULL)
  2030. return TRUE;
  2031. each_size = obj_reloc_entry_size (abfd);
  2032. natsize = (bfd_size_type) each_size * count;
  2033. native = (unsigned char *) bfd_zalloc (abfd, natsize);
  2034. if (!native)
  2035. return FALSE;
  2036. generic = section->orelocation;
  2037. if (each_size == RELOC_EXT_SIZE)
  2038. {
  2039. for (natptr = native;
  2040. count != 0;
  2041. --count, natptr += each_size, ++generic)
  2042. MY_swap_ext_reloc_out (abfd, *generic,
  2043. (struct reloc_ext_external *) natptr);
  2044. }
  2045. else
  2046. {
  2047. for (natptr = native;
  2048. count != 0;
  2049. --count, natptr += each_size, ++generic)
  2050. MY_swap_std_reloc_out (abfd, *generic,
  2051. (struct reloc_std_external *) natptr);
  2052. }
  2053. if (bfd_bwrite ((void *) native, natsize, abfd) != natsize)
  2054. {
  2055. bfd_release (abfd, native);
  2056. return FALSE;
  2057. }
  2058. bfd_release (abfd, native);
  2059. return TRUE;
  2060. }
  2061. /* This is stupid. This function should be a boolean predicate. */
  2062. long
  2063. NAME (aout, canonicalize_reloc) (bfd *abfd,
  2064. sec_ptr section,
  2065. arelent **relptr,
  2066. asymbol **symbols)
  2067. {
  2068. arelent *tblptr = section->relocation;
  2069. unsigned int count;
  2070. if (section == obj_bsssec (abfd))
  2071. {
  2072. *relptr = NULL;
  2073. return 0;
  2074. }
  2075. if (!(tblptr || NAME (aout, slurp_reloc_table) (abfd, section, symbols)))
  2076. return -1;
  2077. if (section->flags & SEC_CONSTRUCTOR)
  2078. {
  2079. arelent_chain *chain = section->constructor_chain;
  2080. for (count = 0; count < section->reloc_count; count ++)
  2081. {
  2082. *relptr ++ = &chain->relent;
  2083. chain = chain->next;
  2084. }
  2085. }
  2086. else
  2087. {
  2088. tblptr = section->relocation;
  2089. for (count = 0; count++ < section->reloc_count; )
  2090. {
  2091. *relptr++ = tblptr++;
  2092. }
  2093. }
  2094. *relptr = 0;
  2095. return section->reloc_count;
  2096. }
  2097. long
  2098. NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
  2099. {
  2100. if (bfd_get_format (abfd) != bfd_object)
  2101. {
  2102. bfd_set_error (bfd_error_invalid_operation);
  2103. return -1;
  2104. }
  2105. if (asect->flags & SEC_CONSTRUCTOR)
  2106. return sizeof (arelent *) * (asect->reloc_count + 1);
  2107. if (asect == obj_datasec (abfd))
  2108. return sizeof (arelent *)
  2109. * ((exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd))
  2110. + 1);
  2111. if (asect == obj_textsec (abfd))
  2112. return sizeof (arelent *)
  2113. * ((exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd))
  2114. + 1);
  2115. if (asect == obj_bsssec (abfd))
  2116. return sizeof (arelent *);
  2117. if (asect == obj_bsssec (abfd))
  2118. return 0;
  2119. bfd_set_error (bfd_error_invalid_operation);
  2120. return -1;
  2121. }
  2122. long
  2123. NAME (aout, get_symtab_upper_bound) (bfd *abfd)
  2124. {
  2125. if (!NAME (aout, slurp_symbol_table) (abfd))
  2126. return -1;
  2127. return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
  2128. }
  2129. alent *
  2130. NAME (aout, get_lineno) (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  2131. asymbol *ignore_symbol ATTRIBUTE_UNUSED)
  2132. {
  2133. return NULL;
  2134. }
  2135. void
  2136. NAME (aout, get_symbol_info) (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  2137. asymbol *symbol,
  2138. symbol_info *ret)
  2139. {
  2140. bfd_symbol_info (symbol, ret);
  2141. if (ret->type == '?')
  2142. {
  2143. int type_code = aout_symbol (symbol)->type & 0xff;
  2144. const char *stab_name = bfd_get_stab_name (type_code);
  2145. static char buf[10];
  2146. if (stab_name == NULL)
  2147. {
  2148. sprintf (buf, "(%d)", type_code);
  2149. stab_name = buf;
  2150. }
  2151. ret->type = '-';
  2152. ret->stab_type = type_code;
  2153. ret->stab_other = (unsigned) (aout_symbol (symbol)->other & 0xff);
  2154. ret->stab_desc = (unsigned) (aout_symbol (symbol)->desc & 0xffff);
  2155. ret->stab_name = stab_name;
  2156. }
  2157. }
  2158. void
  2159. NAME (aout, print_symbol) (bfd *abfd,
  2160. void * afile,
  2161. asymbol *symbol,
  2162. bfd_print_symbol_type how)
  2163. {
  2164. FILE *file = (FILE *)afile;
  2165. switch (how)
  2166. {
  2167. case bfd_print_symbol_name:
  2168. if (symbol->name)
  2169. fprintf (file,"%s", symbol->name);
  2170. break;
  2171. case bfd_print_symbol_more:
  2172. fprintf (file,"%4x %2x %2x",
  2173. (unsigned) (aout_symbol (symbol)->desc & 0xffff),
  2174. (unsigned) (aout_symbol (symbol)->other & 0xff),
  2175. (unsigned) (aout_symbol (symbol)->type));
  2176. break;
  2177. case bfd_print_symbol_all:
  2178. {
  2179. const char *section_name = symbol->section->name;
  2180. bfd_print_symbol_vandf (abfd, (void *)file, symbol);
  2181. fprintf (file," %-5s %04x %02x %02x",
  2182. section_name,
  2183. (unsigned) (aout_symbol (symbol)->desc & 0xffff),
  2184. (unsigned) (aout_symbol (symbol)->other & 0xff),
  2185. (unsigned) (aout_symbol (symbol)->type & 0xff));
  2186. if (symbol->name)
  2187. fprintf (file," %s", symbol->name);
  2188. }
  2189. break;
  2190. }
  2191. }
  2192. /* If we don't have to allocate more than 1MB to hold the generic
  2193. symbols, we use the generic minisymbol methord: it's faster, since
  2194. it only translates the symbols once, not multiple times. */
  2195. #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
  2196. /* Read minisymbols. For minisymbols, we use the unmodified a.out
  2197. symbols. The minisymbol_to_symbol function translates these into
  2198. BFD asymbol structures. */
  2199. long
  2200. NAME (aout, read_minisymbols) (bfd *abfd,
  2201. bfd_boolean dynamic,
  2202. void * *minisymsp,
  2203. unsigned int *sizep)
  2204. {
  2205. if (dynamic)
  2206. /* We could handle the dynamic symbols here as well, but it's
  2207. easier to hand them off. */
  2208. return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
  2209. if (! aout_get_external_symbols (abfd))
  2210. return -1;
  2211. if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
  2212. return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
  2213. *minisymsp = (void *) obj_aout_external_syms (abfd);
  2214. /* By passing the external symbols back from this routine, we are
  2215. giving up control over the memory block. Clear
  2216. obj_aout_external_syms, so that we do not try to free it
  2217. ourselves. */
  2218. obj_aout_external_syms (abfd) = NULL;
  2219. *sizep = EXTERNAL_NLIST_SIZE;
  2220. return obj_aout_external_sym_count (abfd);
  2221. }
  2222. /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
  2223. unmodified a.out symbol. The SYM argument is a structure returned
  2224. by bfd_make_empty_symbol, which we fill in here. */
  2225. asymbol *
  2226. NAME (aout, minisymbol_to_symbol) (bfd *abfd,
  2227. bfd_boolean dynamic,
  2228. const void * minisym,
  2229. asymbol *sym)
  2230. {
  2231. if (dynamic
  2232. || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
  2233. return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
  2234. memset (sym, 0, sizeof (aout_symbol_type));
  2235. /* We call translate_symbol_table to translate a single symbol. */
  2236. if (! (NAME (aout, translate_symbol_table)
  2237. (abfd,
  2238. (aout_symbol_type *) sym,
  2239. (struct external_nlist *) minisym,
  2240. (bfd_size_type) 1,
  2241. obj_aout_external_strings (abfd),
  2242. obj_aout_external_string_size (abfd),
  2243. FALSE)))
  2244. return NULL;
  2245. return sym;
  2246. }
  2247. /* Provided a BFD, a section and an offset into the section, calculate
  2248. and return the name of the source file and the line nearest to the
  2249. wanted location. */
  2250. bfd_boolean
  2251. NAME (aout, find_nearest_line) (bfd *abfd,
  2252. asymbol **symbols,
  2253. asection *section,
  2254. bfd_vma offset,
  2255. const char **filename_ptr,
  2256. const char **functionname_ptr,
  2257. unsigned int *line_ptr,
  2258. unsigned int *disriminator_ptr)
  2259. {
  2260. /* Run down the file looking for the filename, function and linenumber. */
  2261. asymbol **p;
  2262. const char *directory_name = NULL;
  2263. const char *main_file_name = NULL;
  2264. const char *current_file_name = NULL;
  2265. const char *line_file_name = NULL; /* Value of current_file_name at line number. */
  2266. const char *line_directory_name = NULL; /* Value of directory_name at line number. */
  2267. bfd_vma low_line_vma = 0;
  2268. bfd_vma low_func_vma = 0;
  2269. asymbol *func = 0;
  2270. bfd_size_type filelen, funclen;
  2271. char *buf;
  2272. *filename_ptr = abfd->filename;
  2273. *functionname_ptr = 0;
  2274. *line_ptr = 0;
  2275. if (disriminator_ptr)
  2276. *disriminator_ptr = 0;
  2277. if (symbols != NULL)
  2278. {
  2279. for (p = symbols; *p; p++)
  2280. {
  2281. aout_symbol_type *q = (aout_symbol_type *) (*p);
  2282. next:
  2283. switch (q->type)
  2284. {
  2285. case N_TEXT:
  2286. /* If this looks like a file name symbol, and it comes after
  2287. the line number we have found so far, but before the
  2288. offset, then we have probably not found the right line
  2289. number. */
  2290. if (q->symbol.value <= offset
  2291. && ((q->symbol.value > low_line_vma
  2292. && (line_file_name != NULL
  2293. || *line_ptr != 0))
  2294. || (q->symbol.value > low_func_vma
  2295. && func != NULL)))
  2296. {
  2297. const char *symname;
  2298. symname = q->symbol.name;
  2299. if (strcmp (symname + strlen (symname) - 2, ".o") == 0)
  2300. {
  2301. if (q->symbol.value > low_line_vma)
  2302. {
  2303. *line_ptr = 0;
  2304. line_file_name = NULL;
  2305. }
  2306. if (q->symbol.value > low_func_vma)
  2307. func = NULL;
  2308. }
  2309. }
  2310. break;
  2311. case N_SO:
  2312. /* If this symbol is less than the offset, but greater than
  2313. the line number we have found so far, then we have not
  2314. found the right line number. */
  2315. if (q->symbol.value <= offset)
  2316. {
  2317. if (q->symbol.value > low_line_vma)
  2318. {
  2319. *line_ptr = 0;
  2320. line_file_name = NULL;
  2321. }
  2322. if (q->symbol.value > low_func_vma)
  2323. func = NULL;
  2324. }
  2325. main_file_name = current_file_name = q->symbol.name;
  2326. /* Look ahead to next symbol to check if that too is an N_SO. */
  2327. p++;
  2328. if (*p == NULL)
  2329. goto done;
  2330. q = (aout_symbol_type *) (*p);
  2331. if (q->type != (int)N_SO)
  2332. goto next;
  2333. /* Found a second N_SO First is directory; second is filename. */
  2334. directory_name = current_file_name;
  2335. main_file_name = current_file_name = q->symbol.name;
  2336. if (obj_textsec (abfd) != section)
  2337. goto done;
  2338. break;
  2339. case N_SOL:
  2340. current_file_name = q->symbol.name;
  2341. break;
  2342. case N_SLINE:
  2343. case N_DSLINE:
  2344. case N_BSLINE:
  2345. /* We'll keep this if it resolves nearer than the one we have
  2346. already. */
  2347. if (q->symbol.value >= low_line_vma
  2348. && q->symbol.value <= offset)
  2349. {
  2350. *line_ptr = q->desc;
  2351. low_line_vma = q->symbol.value;
  2352. line_file_name = current_file_name;
  2353. line_directory_name = directory_name;
  2354. }
  2355. break;
  2356. case N_FUN:
  2357. {
  2358. /* We'll keep this if it is nearer than the one we have already. */
  2359. if (q->symbol.value >= low_func_vma &&
  2360. q->symbol.value <= offset)
  2361. {
  2362. low_func_vma = q->symbol.value;
  2363. func = (asymbol *)q;
  2364. }
  2365. else if (q->symbol.value > offset)
  2366. goto done;
  2367. }
  2368. break;
  2369. }
  2370. }
  2371. }
  2372. done:
  2373. if (*line_ptr != 0)
  2374. {
  2375. main_file_name = line_file_name;
  2376. directory_name = line_directory_name;
  2377. }
  2378. if (main_file_name == NULL
  2379. || IS_ABSOLUTE_PATH (main_file_name)
  2380. || directory_name == NULL)
  2381. filelen = 0;
  2382. else
  2383. filelen = strlen (directory_name) + strlen (main_file_name);
  2384. if (func == NULL)
  2385. funclen = 0;
  2386. else
  2387. funclen = strlen (bfd_asymbol_name (func));
  2388. if (adata (abfd).line_buf != NULL)
  2389. free (adata (abfd).line_buf);
  2390. if (filelen + funclen == 0)
  2391. adata (abfd).line_buf = buf = NULL;
  2392. else
  2393. {
  2394. buf = (char *) bfd_malloc (filelen + funclen + 3);
  2395. adata (abfd).line_buf = buf;
  2396. if (buf == NULL)
  2397. return FALSE;
  2398. }
  2399. if (main_file_name != NULL)
  2400. {
  2401. if (IS_ABSOLUTE_PATH (main_file_name) || directory_name == NULL)
  2402. *filename_ptr = main_file_name;
  2403. else
  2404. {
  2405. sprintf (buf, "%s%s", directory_name, main_file_name);
  2406. *filename_ptr = buf;
  2407. buf += filelen + 1;
  2408. }
  2409. }
  2410. if (func)
  2411. {
  2412. const char *function = func->name;
  2413. char *colon;
  2414. /* The caller expects a symbol name. We actually have a
  2415. function name, without the leading underscore. Put the
  2416. underscore back in, so that the caller gets a symbol name. */
  2417. if (bfd_get_symbol_leading_char (abfd) == '\0')
  2418. strcpy (buf, function);
  2419. else
  2420. {
  2421. buf[0] = bfd_get_symbol_leading_char (abfd);
  2422. strcpy (buf + 1, function);
  2423. }
  2424. /* Have to remove : stuff. */
  2425. colon = strchr (buf, ':');
  2426. if (colon != NULL)
  2427. *colon = '\0';
  2428. *functionname_ptr = buf;
  2429. }
  2430. return TRUE;
  2431. }
  2432. int
  2433. NAME (aout, sizeof_headers) (bfd *abfd,
  2434. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2435. {
  2436. return adata (abfd).exec_bytes_size;
  2437. }
  2438. /* Free all information we have cached for this BFD. We can always
  2439. read it again later if we need it. */
  2440. bfd_boolean
  2441. NAME (aout, bfd_free_cached_info) (bfd *abfd)
  2442. {
  2443. asection *o;
  2444. if (bfd_get_format (abfd) != bfd_object
  2445. || abfd->tdata.aout_data == NULL)
  2446. return TRUE;
  2447. #define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
  2448. BFCI_FREE (obj_aout_symbols (abfd));
  2449. #ifdef USE_MMAP
  2450. obj_aout_external_syms (abfd) = 0;
  2451. bfd_free_window (&obj_aout_sym_window (abfd));
  2452. bfd_free_window (&obj_aout_string_window (abfd));
  2453. obj_aout_external_strings (abfd) = 0;
  2454. #else
  2455. BFCI_FREE (obj_aout_external_syms (abfd));
  2456. BFCI_FREE (obj_aout_external_strings (abfd));
  2457. #endif
  2458. for (o = abfd->sections; o != NULL; o = o->next)
  2459. BFCI_FREE (o->relocation);
  2460. #undef BFCI_FREE
  2461. return TRUE;
  2462. }
  2463. /* a.out link code. */
  2464. /* Routine to create an entry in an a.out link hash table. */
  2465. struct bfd_hash_entry *
  2466. NAME (aout, link_hash_newfunc) (struct bfd_hash_entry *entry,
  2467. struct bfd_hash_table *table,
  2468. const char *string)
  2469. {
  2470. struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
  2471. /* Allocate the structure if it has not already been allocated by a
  2472. subclass. */
  2473. if (ret == NULL)
  2474. ret = (struct aout_link_hash_entry *) bfd_hash_allocate (table,
  2475. sizeof (* ret));
  2476. if (ret == NULL)
  2477. return NULL;
  2478. /* Call the allocation method of the superclass. */
  2479. ret = ((struct aout_link_hash_entry *)
  2480. _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  2481. table, string));
  2482. if (ret)
  2483. {
  2484. /* Set local fields. */
  2485. ret->written = FALSE;
  2486. ret->indx = -1;
  2487. }
  2488. return (struct bfd_hash_entry *) ret;
  2489. }
  2490. /* Initialize an a.out link hash table. */
  2491. bfd_boolean
  2492. NAME (aout, link_hash_table_init) (struct aout_link_hash_table *table,
  2493. bfd *abfd,
  2494. struct bfd_hash_entry *(*newfunc)
  2495. (struct bfd_hash_entry *, struct bfd_hash_table *,
  2496. const char *),
  2497. unsigned int entsize)
  2498. {
  2499. return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
  2500. }
  2501. /* Create an a.out link hash table. */
  2502. struct bfd_link_hash_table *
  2503. NAME (aout, link_hash_table_create) (bfd *abfd)
  2504. {
  2505. struct aout_link_hash_table *ret;
  2506. bfd_size_type amt = sizeof (* ret);
  2507. ret = (struct aout_link_hash_table *) bfd_malloc (amt);
  2508. if (ret == NULL)
  2509. return NULL;
  2510. if (!NAME (aout, link_hash_table_init) (ret, abfd,
  2511. NAME (aout, link_hash_newfunc),
  2512. sizeof (struct aout_link_hash_entry)))
  2513. {
  2514. free (ret);
  2515. return NULL;
  2516. }
  2517. return &ret->root;
  2518. }
  2519. /* Add all symbols from an object file to the hash table. */
  2520. static bfd_boolean
  2521. aout_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  2522. {
  2523. bfd_boolean (*add_one_symbol)
  2524. (struct bfd_link_info *, bfd *, const char *, flagword, asection *,
  2525. bfd_vma, const char *, bfd_boolean, bfd_boolean,
  2526. struct bfd_link_hash_entry **);
  2527. struct external_nlist *syms;
  2528. bfd_size_type sym_count;
  2529. char *strings;
  2530. bfd_boolean copy;
  2531. struct aout_link_hash_entry **sym_hash;
  2532. struct external_nlist *p;
  2533. struct external_nlist *pend;
  2534. bfd_size_type amt;
  2535. syms = obj_aout_external_syms (abfd);
  2536. sym_count = obj_aout_external_sym_count (abfd);
  2537. strings = obj_aout_external_strings (abfd);
  2538. if (info->keep_memory)
  2539. copy = FALSE;
  2540. else
  2541. copy = TRUE;
  2542. if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
  2543. {
  2544. if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
  2545. (abfd, info, &syms, &sym_count, &strings)))
  2546. return FALSE;
  2547. }
  2548. if (sym_count == 0)
  2549. return TRUE; /* Nothing to do. */
  2550. /* We keep a list of the linker hash table entries that correspond
  2551. to particular symbols. We could just look them up in the hash
  2552. table, but keeping the list is more efficient. Perhaps this
  2553. should be conditional on info->keep_memory. */
  2554. amt = sym_count * sizeof (struct aout_link_hash_entry *);
  2555. sym_hash = (struct aout_link_hash_entry **) bfd_alloc (abfd, amt);
  2556. if (sym_hash == NULL)
  2557. return FALSE;
  2558. obj_aout_sym_hashes (abfd) = sym_hash;
  2559. add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
  2560. if (add_one_symbol == NULL)
  2561. add_one_symbol = _bfd_generic_link_add_one_symbol;
  2562. p = syms;
  2563. pend = p + sym_count;
  2564. for (; p < pend; p++, sym_hash++)
  2565. {
  2566. int type;
  2567. const char *name;
  2568. bfd_vma value;
  2569. asection *section;
  2570. flagword flags;
  2571. const char *string;
  2572. *sym_hash = NULL;
  2573. type = H_GET_8 (abfd, p->e_type);
  2574. /* Ignore debugging symbols. */
  2575. if ((type & N_STAB) != 0)
  2576. continue;
  2577. name = strings + GET_WORD (abfd, p->e_strx);
  2578. value = GET_WORD (abfd, p->e_value);
  2579. flags = BSF_GLOBAL;
  2580. string = NULL;
  2581. switch (type)
  2582. {
  2583. default:
  2584. abort ();
  2585. case N_UNDF:
  2586. case N_ABS:
  2587. case N_TEXT:
  2588. case N_DATA:
  2589. case N_BSS:
  2590. case N_FN_SEQ:
  2591. case N_COMM:
  2592. case N_SETV:
  2593. case N_FN:
  2594. /* Ignore symbols that are not externally visible. */
  2595. continue;
  2596. case N_INDR:
  2597. /* Ignore local indirect symbol. */
  2598. ++p;
  2599. ++sym_hash;
  2600. continue;
  2601. case N_UNDF | N_EXT:
  2602. if (value == 0)
  2603. {
  2604. section = bfd_und_section_ptr;
  2605. flags = 0;
  2606. }
  2607. else
  2608. section = bfd_com_section_ptr;
  2609. break;
  2610. case N_ABS | N_EXT:
  2611. section = bfd_abs_section_ptr;
  2612. break;
  2613. case N_TEXT | N_EXT:
  2614. section = obj_textsec (abfd);
  2615. value -= bfd_get_section_vma (abfd, section);
  2616. break;
  2617. case N_DATA | N_EXT:
  2618. case N_SETV | N_EXT:
  2619. /* Treat N_SETV symbols as N_DATA symbol; see comment in
  2620. translate_from_native_sym_flags. */
  2621. section = obj_datasec (abfd);
  2622. value -= bfd_get_section_vma (abfd, section);
  2623. break;
  2624. case N_BSS | N_EXT:
  2625. section = obj_bsssec (abfd);
  2626. value -= bfd_get_section_vma (abfd, section);
  2627. break;
  2628. case N_INDR | N_EXT:
  2629. /* An indirect symbol. The next symbol is the symbol
  2630. which this one really is. */
  2631. BFD_ASSERT (p + 1 < pend);
  2632. ++p;
  2633. string = strings + GET_WORD (abfd, p->e_strx);
  2634. section = bfd_ind_section_ptr;
  2635. flags |= BSF_INDIRECT;
  2636. break;
  2637. case N_COMM | N_EXT:
  2638. section = bfd_com_section_ptr;
  2639. break;
  2640. case N_SETA: case N_SETA | N_EXT:
  2641. section = bfd_abs_section_ptr;
  2642. flags |= BSF_CONSTRUCTOR;
  2643. break;
  2644. case N_SETT: case N_SETT | N_EXT:
  2645. section = obj_textsec (abfd);
  2646. flags |= BSF_CONSTRUCTOR;
  2647. value -= bfd_get_section_vma (abfd, section);
  2648. break;
  2649. case N_SETD: case N_SETD | N_EXT:
  2650. section = obj_datasec (abfd);
  2651. flags |= BSF_CONSTRUCTOR;
  2652. value -= bfd_get_section_vma (abfd, section);
  2653. break;
  2654. case N_SETB: case N_SETB | N_EXT:
  2655. section = obj_bsssec (abfd);
  2656. flags |= BSF_CONSTRUCTOR;
  2657. value -= bfd_get_section_vma (abfd, section);
  2658. break;
  2659. case N_WARNING:
  2660. /* A warning symbol. The next symbol is the one to warn
  2661. about. If there is no next symbol, just look away. */
  2662. if (p + 1 >= pend)
  2663. return TRUE;
  2664. ++p;
  2665. string = name;
  2666. name = strings + GET_WORD (abfd, p->e_strx);
  2667. section = bfd_und_section_ptr;
  2668. flags |= BSF_WARNING;
  2669. break;
  2670. case N_WEAKU:
  2671. section = bfd_und_section_ptr;
  2672. flags = BSF_WEAK;
  2673. break;
  2674. case N_WEAKA:
  2675. section = bfd_abs_section_ptr;
  2676. flags = BSF_WEAK;
  2677. break;
  2678. case N_WEAKT:
  2679. section = obj_textsec (abfd);
  2680. value -= bfd_get_section_vma (abfd, section);
  2681. flags = BSF_WEAK;
  2682. break;
  2683. case N_WEAKD:
  2684. section = obj_datasec (abfd);
  2685. value -= bfd_get_section_vma (abfd, section);
  2686. flags = BSF_WEAK;
  2687. break;
  2688. case N_WEAKB:
  2689. section = obj_bsssec (abfd);
  2690. value -= bfd_get_section_vma (abfd, section);
  2691. flags = BSF_WEAK;
  2692. break;
  2693. }
  2694. if (! ((*add_one_symbol)
  2695. (info, abfd, name, flags, section, value, string, copy, FALSE,
  2696. (struct bfd_link_hash_entry **) sym_hash)))
  2697. return FALSE;
  2698. /* Restrict the maximum alignment of a common symbol based on
  2699. the architecture, since a.out has no way to represent
  2700. alignment requirements of a section in a .o file. FIXME:
  2701. This isn't quite right: it should use the architecture of the
  2702. output file, not the input files. */
  2703. if ((*sym_hash)->root.type == bfd_link_hash_common
  2704. && ((*sym_hash)->root.u.c.p->alignment_power >
  2705. bfd_get_arch_info (abfd)->section_align_power))
  2706. (*sym_hash)->root.u.c.p->alignment_power =
  2707. bfd_get_arch_info (abfd)->section_align_power;
  2708. /* If this is a set symbol, and we are not building sets, then
  2709. it is possible for the hash entry to not have been set. In
  2710. such a case, treat the symbol as not globally defined. */
  2711. if ((*sym_hash)->root.type == bfd_link_hash_new)
  2712. {
  2713. BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
  2714. *sym_hash = NULL;
  2715. }
  2716. if (type == (N_INDR | N_EXT) || type == N_WARNING)
  2717. ++sym_hash;
  2718. }
  2719. return TRUE;
  2720. }
  2721. /* Free up the internal symbols read from an a.out file. */
  2722. static bfd_boolean
  2723. aout_link_free_symbols (bfd *abfd)
  2724. {
  2725. if (obj_aout_external_syms (abfd) != NULL)
  2726. {
  2727. #ifdef USE_MMAP
  2728. bfd_free_window (&obj_aout_sym_window (abfd));
  2729. #else
  2730. free ((void *) obj_aout_external_syms (abfd));
  2731. #endif
  2732. obj_aout_external_syms (abfd) = NULL;
  2733. }
  2734. if (obj_aout_external_strings (abfd) != NULL)
  2735. {
  2736. #ifdef USE_MMAP
  2737. bfd_free_window (&obj_aout_string_window (abfd));
  2738. #else
  2739. free ((void *) obj_aout_external_strings (abfd));
  2740. #endif
  2741. obj_aout_external_strings (abfd) = NULL;
  2742. }
  2743. return TRUE;
  2744. }
  2745. /* Add symbols from an a.out object file. */
  2746. static bfd_boolean
  2747. aout_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
  2748. {
  2749. if (! aout_get_external_symbols (abfd))
  2750. return FALSE;
  2751. if (! aout_link_add_symbols (abfd, info))
  2752. return FALSE;
  2753. if (! info->keep_memory)
  2754. {
  2755. if (! aout_link_free_symbols (abfd))
  2756. return FALSE;
  2757. }
  2758. return TRUE;
  2759. }
  2760. /* Look through the internal symbols to see if this object file should
  2761. be included in the link. We should include this object file if it
  2762. defines any symbols which are currently undefined. If this object
  2763. file defines a common symbol, then we may adjust the size of the
  2764. known symbol but we do not include the object file in the link
  2765. (unless there is some other reason to include it). */
  2766. static bfd_boolean
  2767. aout_link_check_ar_symbols (bfd *abfd,
  2768. struct bfd_link_info *info,
  2769. bfd_boolean *pneeded,
  2770. bfd **subsbfd)
  2771. {
  2772. struct external_nlist *p;
  2773. struct external_nlist *pend;
  2774. char *strings;
  2775. *pneeded = FALSE;
  2776. /* Look through all the symbols. */
  2777. p = obj_aout_external_syms (abfd);
  2778. pend = p + obj_aout_external_sym_count (abfd);
  2779. strings = obj_aout_external_strings (abfd);
  2780. for (; p < pend; p++)
  2781. {
  2782. int type = H_GET_8 (abfd, p->e_type);
  2783. const char *name;
  2784. struct bfd_link_hash_entry *h;
  2785. /* Ignore symbols that are not externally visible. This is an
  2786. optimization only, as we check the type more thoroughly
  2787. below. */
  2788. if (((type & N_EXT) == 0
  2789. || (type & N_STAB) != 0
  2790. || type == N_FN)
  2791. && type != N_WEAKA
  2792. && type != N_WEAKT
  2793. && type != N_WEAKD
  2794. && type != N_WEAKB)
  2795. {
  2796. if (type == N_WARNING
  2797. || type == N_INDR)
  2798. ++p;
  2799. continue;
  2800. }
  2801. name = strings + GET_WORD (abfd, p->e_strx);
  2802. h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
  2803. /* We are only interested in symbols that are currently
  2804. undefined or common. */
  2805. if (h == NULL
  2806. || (h->type != bfd_link_hash_undefined
  2807. && h->type != bfd_link_hash_common))
  2808. {
  2809. if (type == (N_INDR | N_EXT))
  2810. ++p;
  2811. continue;
  2812. }
  2813. if (type == (N_TEXT | N_EXT)
  2814. || type == (N_DATA | N_EXT)
  2815. || type == (N_BSS | N_EXT)
  2816. || type == (N_ABS | N_EXT)
  2817. || type == (N_INDR | N_EXT))
  2818. {
  2819. /* This object file defines this symbol. We must link it
  2820. in. This is true regardless of whether the current
  2821. definition of the symbol is undefined or common.
  2822. If the current definition is common, we have a case in
  2823. which we have already seen an object file including:
  2824. int a;
  2825. and this object file from the archive includes:
  2826. int a = 5;
  2827. In such a case, whether to include this object is target
  2828. dependant for backward compatibility.
  2829. FIXME: The SunOS 4.1.3 linker will pull in the archive
  2830. element if the symbol is defined in the .data section,
  2831. but not if it is defined in the .text section. That
  2832. seems a bit crazy to me, and it has not been implemented
  2833. yet. However, it might be correct. */
  2834. if (h->type == bfd_link_hash_common)
  2835. {
  2836. int skip = 0;
  2837. switch (info->common_skip_ar_symbols)
  2838. {
  2839. case bfd_link_common_skip_text:
  2840. skip = (type == (N_TEXT | N_EXT));
  2841. break;
  2842. case bfd_link_common_skip_data:
  2843. skip = (type == (N_DATA | N_EXT));
  2844. break;
  2845. default:
  2846. case bfd_link_common_skip_all:
  2847. skip = 1;
  2848. break;
  2849. }
  2850. if (skip)
  2851. continue;
  2852. }
  2853. if (!(*info->callbacks
  2854. ->add_archive_element) (info, abfd, name, subsbfd))
  2855. return FALSE;
  2856. *pneeded = TRUE;
  2857. return TRUE;
  2858. }
  2859. if (type == (N_UNDF | N_EXT))
  2860. {
  2861. bfd_vma value;
  2862. value = GET_WORD (abfd, p->e_value);
  2863. if (value != 0)
  2864. {
  2865. /* This symbol is common in the object from the archive
  2866. file. */
  2867. if (h->type == bfd_link_hash_undefined)
  2868. {
  2869. bfd *symbfd;
  2870. unsigned int power;
  2871. symbfd = h->u.undef.abfd;
  2872. if (symbfd == NULL)
  2873. {
  2874. /* This symbol was created as undefined from
  2875. outside BFD. We assume that we should link
  2876. in the object file. This is done for the -u
  2877. option in the linker. */
  2878. if (!(*info->callbacks
  2879. ->add_archive_element) (info, abfd, name, subsbfd))
  2880. return FALSE;
  2881. *pneeded = TRUE;
  2882. return TRUE;
  2883. }
  2884. /* Turn the current link symbol into a common
  2885. symbol. It is already on the undefs list. */
  2886. h->type = bfd_link_hash_common;
  2887. h->u.c.p = (struct bfd_link_hash_common_entry *)
  2888. bfd_hash_allocate (&info->hash->table,
  2889. sizeof (struct bfd_link_hash_common_entry));
  2890. if (h->u.c.p == NULL)
  2891. return FALSE;
  2892. h->u.c.size = value;
  2893. /* FIXME: This isn't quite right. The maximum
  2894. alignment of a common symbol should be set by the
  2895. architecture of the output file, not of the input
  2896. file. */
  2897. power = bfd_log2 (value);
  2898. if (power > bfd_get_arch_info (abfd)->section_align_power)
  2899. power = bfd_get_arch_info (abfd)->section_align_power;
  2900. h->u.c.p->alignment_power = power;
  2901. h->u.c.p->section = bfd_make_section_old_way (symbfd,
  2902. "COMMON");
  2903. }
  2904. else
  2905. {
  2906. /* Adjust the size of the common symbol if
  2907. necessary. */
  2908. if (value > h->u.c.size)
  2909. h->u.c.size = value;
  2910. }
  2911. }
  2912. }
  2913. if (type == N_WEAKA
  2914. || type == N_WEAKT
  2915. || type == N_WEAKD
  2916. || type == N_WEAKB)
  2917. {
  2918. /* This symbol is weak but defined. We must pull it in if
  2919. the current link symbol is undefined, but we don't want
  2920. it if the current link symbol is common. */
  2921. if (h->type == bfd_link_hash_undefined)
  2922. {
  2923. if (!(*info->callbacks
  2924. ->add_archive_element) (info, abfd, name, subsbfd))
  2925. return FALSE;
  2926. *pneeded = TRUE;
  2927. return TRUE;
  2928. }
  2929. }
  2930. }
  2931. /* We do not need this object file. */
  2932. return TRUE;
  2933. }
  2934. /* Check a single archive element to see if we need to include it in
  2935. the link. *PNEEDED is set according to whether this element is
  2936. needed in the link or not. This is called from
  2937. _bfd_generic_link_add_archive_symbols. */
  2938. static bfd_boolean
  2939. aout_link_check_archive_element (bfd *abfd,
  2940. struct bfd_link_info *info,
  2941. struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
  2942. const char *name ATTRIBUTE_UNUSED,
  2943. bfd_boolean *pneeded)
  2944. {
  2945. bfd *oldbfd;
  2946. bfd_boolean needed;
  2947. if (!aout_get_external_symbols (abfd))
  2948. return FALSE;
  2949. oldbfd = abfd;
  2950. if (!aout_link_check_ar_symbols (abfd, info, pneeded, &abfd))
  2951. return FALSE;
  2952. needed = *pneeded;
  2953. if (needed)
  2954. {
  2955. /* Potentially, the add_archive_element hook may have set a
  2956. substitute BFD for us. */
  2957. if (abfd != oldbfd)
  2958. {
  2959. if (!info->keep_memory
  2960. && !aout_link_free_symbols (oldbfd))
  2961. return FALSE;
  2962. if (!aout_get_external_symbols (abfd))
  2963. return FALSE;
  2964. }
  2965. if (!aout_link_add_symbols (abfd, info))
  2966. return FALSE;
  2967. }
  2968. if (!info->keep_memory || !needed)
  2969. {
  2970. if (!aout_link_free_symbols (abfd))
  2971. return FALSE;
  2972. }
  2973. return TRUE;
  2974. }
  2975. /* Given an a.out BFD, add symbols to the global hash table as
  2976. appropriate. */
  2977. bfd_boolean
  2978. NAME (aout, link_add_symbols) (bfd *abfd, struct bfd_link_info *info)
  2979. {
  2980. switch (bfd_get_format (abfd))
  2981. {
  2982. case bfd_object:
  2983. return aout_link_add_object_symbols (abfd, info);
  2984. case bfd_archive:
  2985. return _bfd_generic_link_add_archive_symbols
  2986. (abfd, info, aout_link_check_archive_element);
  2987. default:
  2988. bfd_set_error (bfd_error_wrong_format);
  2989. return FALSE;
  2990. }
  2991. }
  2992. /* A hash table used for header files with N_BINCL entries. */
  2993. struct aout_link_includes_table
  2994. {
  2995. struct bfd_hash_table root;
  2996. };
  2997. /* A linked list of totals that we have found for a particular header
  2998. file. */
  2999. struct aout_link_includes_totals
  3000. {
  3001. struct aout_link_includes_totals *next;
  3002. bfd_vma total;
  3003. };
  3004. /* An entry in the header file hash table. */
  3005. struct aout_link_includes_entry
  3006. {
  3007. struct bfd_hash_entry root;
  3008. /* List of totals we have found for this file. */
  3009. struct aout_link_includes_totals *totals;
  3010. };
  3011. /* Look up an entry in an the header file hash table. */
  3012. #define aout_link_includes_lookup(table, string, create, copy) \
  3013. ((struct aout_link_includes_entry *) \
  3014. bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
  3015. /* During the final link step we need to pass around a bunch of
  3016. information, so we do it in an instance of this structure. */
  3017. struct aout_final_link_info
  3018. {
  3019. /* General link information. */
  3020. struct bfd_link_info *info;
  3021. /* Output bfd. */
  3022. bfd *output_bfd;
  3023. /* Reloc file positions. */
  3024. file_ptr treloff, dreloff;
  3025. /* File position of symbols. */
  3026. file_ptr symoff;
  3027. /* String table. */
  3028. struct bfd_strtab_hash *strtab;
  3029. /* Header file hash table. */
  3030. struct aout_link_includes_table includes;
  3031. /* A buffer large enough to hold the contents of any section. */
  3032. bfd_byte *contents;
  3033. /* A buffer large enough to hold the relocs of any section. */
  3034. void * relocs;
  3035. /* A buffer large enough to hold the symbol map of any input BFD. */
  3036. int *symbol_map;
  3037. /* A buffer large enough to hold output symbols of any input BFD. */
  3038. struct external_nlist *output_syms;
  3039. };
  3040. /* The function to create a new entry in the header file hash table. */
  3041. static struct bfd_hash_entry *
  3042. aout_link_includes_newfunc (struct bfd_hash_entry *entry,
  3043. struct bfd_hash_table *table,
  3044. const char *string)
  3045. {
  3046. struct aout_link_includes_entry *ret =
  3047. (struct aout_link_includes_entry *) entry;
  3048. /* Allocate the structure if it has not already been allocated by a
  3049. subclass. */
  3050. if (ret == NULL)
  3051. ret = (struct aout_link_includes_entry *)
  3052. bfd_hash_allocate (table, sizeof (* ret));
  3053. if (ret == NULL)
  3054. return NULL;
  3055. /* Call the allocation method of the superclass. */
  3056. ret = ((struct aout_link_includes_entry *)
  3057. bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  3058. if (ret)
  3059. {
  3060. /* Set local fields. */
  3061. ret->totals = NULL;
  3062. }
  3063. return (struct bfd_hash_entry *) ret;
  3064. }
  3065. /* Write out a symbol that was not associated with an a.out input
  3066. object. */
  3067. static bfd_boolean
  3068. aout_link_write_other_symbol (struct bfd_hash_entry *bh, void *data)
  3069. {
  3070. struct aout_link_hash_entry *h = (struct aout_link_hash_entry *) bh;
  3071. struct aout_final_link_info *flaginfo = (struct aout_final_link_info *) data;
  3072. bfd *output_bfd;
  3073. int type;
  3074. bfd_vma val;
  3075. struct external_nlist outsym;
  3076. bfd_size_type indx;
  3077. bfd_size_type amt;
  3078. if (h->root.type == bfd_link_hash_warning)
  3079. {
  3080. h = (struct aout_link_hash_entry *) h->root.u.i.link;
  3081. if (h->root.type == bfd_link_hash_new)
  3082. return TRUE;
  3083. }
  3084. output_bfd = flaginfo->output_bfd;
  3085. if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
  3086. {
  3087. if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
  3088. (output_bfd, flaginfo->info, h)))
  3089. {
  3090. /* FIXME: No way to handle errors. */
  3091. abort ();
  3092. }
  3093. }
  3094. if (h->written)
  3095. return TRUE;
  3096. h->written = TRUE;
  3097. /* An indx of -2 means the symbol must be written. */
  3098. if (h->indx != -2
  3099. && (flaginfo->info->strip == strip_all
  3100. || (flaginfo->info->strip == strip_some
  3101. && bfd_hash_lookup (flaginfo->info->keep_hash, h->root.root.string,
  3102. FALSE, FALSE) == NULL)))
  3103. return TRUE;
  3104. switch (h->root.type)
  3105. {
  3106. default:
  3107. case bfd_link_hash_warning:
  3108. abort ();
  3109. /* Avoid variable not initialized warnings. */
  3110. return TRUE;
  3111. case bfd_link_hash_new:
  3112. /* This can happen for set symbols when sets are not being
  3113. built. */
  3114. return TRUE;
  3115. case bfd_link_hash_undefined:
  3116. type = N_UNDF | N_EXT;
  3117. val = 0;
  3118. break;
  3119. case bfd_link_hash_defined:
  3120. case bfd_link_hash_defweak:
  3121. {
  3122. asection *sec;
  3123. sec = h->root.u.def.section->output_section;
  3124. BFD_ASSERT (bfd_is_abs_section (sec)
  3125. || sec->owner == output_bfd);
  3126. if (sec == obj_textsec (output_bfd))
  3127. type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
  3128. else if (sec == obj_datasec (output_bfd))
  3129. type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
  3130. else if (sec == obj_bsssec (output_bfd))
  3131. type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
  3132. else
  3133. type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
  3134. type |= N_EXT;
  3135. val = (h->root.u.def.value
  3136. + sec->vma
  3137. + h->root.u.def.section->output_offset);
  3138. }
  3139. break;
  3140. case bfd_link_hash_common:
  3141. type = N_UNDF | N_EXT;
  3142. val = h->root.u.c.size;
  3143. break;
  3144. case bfd_link_hash_undefweak:
  3145. type = N_WEAKU;
  3146. val = 0;
  3147. break;
  3148. case bfd_link_hash_indirect:
  3149. /* We ignore these symbols, since the indirected symbol is
  3150. already in the hash table. */
  3151. return TRUE;
  3152. }
  3153. H_PUT_8 (output_bfd, type, outsym.e_type);
  3154. H_PUT_8 (output_bfd, 0, outsym.e_other);
  3155. H_PUT_16 (output_bfd, 0, outsym.e_desc);
  3156. indx = add_to_stringtab (output_bfd, flaginfo->strtab, h->root.root.string,
  3157. FALSE);
  3158. if (indx == - (bfd_size_type) 1)
  3159. /* FIXME: No way to handle errors. */
  3160. abort ();
  3161. PUT_WORD (output_bfd, indx, outsym.e_strx);
  3162. PUT_WORD (output_bfd, val, outsym.e_value);
  3163. amt = EXTERNAL_NLIST_SIZE;
  3164. if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0
  3165. || bfd_bwrite ((void *) &outsym, amt, output_bfd) != amt)
  3166. /* FIXME: No way to handle errors. */
  3167. abort ();
  3168. flaginfo->symoff += EXTERNAL_NLIST_SIZE;
  3169. h->indx = obj_aout_external_sym_count (output_bfd);
  3170. ++obj_aout_external_sym_count (output_bfd);
  3171. return TRUE;
  3172. }
  3173. /* Handle a link order which is supposed to generate a reloc. */
  3174. static bfd_boolean
  3175. aout_link_reloc_link_order (struct aout_final_link_info *flaginfo,
  3176. asection *o,
  3177. struct bfd_link_order *p)
  3178. {
  3179. struct bfd_link_order_reloc *pr;
  3180. int r_index;
  3181. int r_extern;
  3182. reloc_howto_type *howto;
  3183. file_ptr *reloff_ptr = NULL;
  3184. struct reloc_std_external srel;
  3185. struct reloc_ext_external erel;
  3186. void * rel_ptr;
  3187. bfd_size_type amt;
  3188. pr = p->u.reloc.p;
  3189. if (p->type == bfd_section_reloc_link_order)
  3190. {
  3191. r_extern = 0;
  3192. if (bfd_is_abs_section (pr->u.section))
  3193. r_index = N_ABS | N_EXT;
  3194. else
  3195. {
  3196. BFD_ASSERT (pr->u.section->owner == flaginfo->output_bfd);
  3197. r_index = pr->u.section->target_index;
  3198. }
  3199. }
  3200. else
  3201. {
  3202. struct aout_link_hash_entry *h;
  3203. BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
  3204. r_extern = 1;
  3205. h = ((struct aout_link_hash_entry *)
  3206. bfd_wrapped_link_hash_lookup (flaginfo->output_bfd, flaginfo->info,
  3207. pr->u.name, FALSE, FALSE, TRUE));
  3208. if (h != NULL
  3209. && h->indx >= 0)
  3210. r_index = h->indx;
  3211. else if (h != NULL)
  3212. {
  3213. /* We decided to strip this symbol, but it turns out that we
  3214. can't. Note that we lose the other and desc information
  3215. here. I don't think that will ever matter for a global
  3216. symbol. */
  3217. h->indx = -2;
  3218. h->written = FALSE;
  3219. if (!aout_link_write_other_symbol (&h->root.root, flaginfo))
  3220. return FALSE;
  3221. r_index = h->indx;
  3222. }
  3223. else
  3224. {
  3225. if (! ((*flaginfo->info->callbacks->unattached_reloc)
  3226. (flaginfo->info, pr->u.name, NULL, NULL, (bfd_vma) 0)))
  3227. return FALSE;
  3228. r_index = 0;
  3229. }
  3230. }
  3231. howto = bfd_reloc_type_lookup (flaginfo->output_bfd, pr->reloc);
  3232. if (howto == 0)
  3233. {
  3234. bfd_set_error (bfd_error_bad_value);
  3235. return FALSE;
  3236. }
  3237. if (o == obj_textsec (flaginfo->output_bfd))
  3238. reloff_ptr = &flaginfo->treloff;
  3239. else if (o == obj_datasec (flaginfo->output_bfd))
  3240. reloff_ptr = &flaginfo->dreloff;
  3241. else
  3242. abort ();
  3243. if (obj_reloc_entry_size (flaginfo->output_bfd) == RELOC_STD_SIZE)
  3244. {
  3245. #ifdef MY_put_reloc
  3246. MY_put_reloc (flaginfo->output_bfd, r_extern, r_index, p->offset, howto,
  3247. &srel);
  3248. #else
  3249. {
  3250. int r_pcrel;
  3251. int r_baserel;
  3252. int r_jmptable;
  3253. int r_relative;
  3254. int r_length;
  3255. r_pcrel = (int) howto->pc_relative;
  3256. r_baserel = (howto->type & 8) != 0;
  3257. r_jmptable = (howto->type & 16) != 0;
  3258. r_relative = (howto->type & 32) != 0;
  3259. r_length = howto->size;
  3260. PUT_WORD (flaginfo->output_bfd, p->offset, srel.r_address);
  3261. if (bfd_header_big_endian (flaginfo->output_bfd))
  3262. {
  3263. srel.r_index[0] = r_index >> 16;
  3264. srel.r_index[1] = r_index >> 8;
  3265. srel.r_index[2] = r_index;
  3266. srel.r_type[0] =
  3267. ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
  3268. | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
  3269. | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
  3270. | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
  3271. | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
  3272. | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
  3273. }
  3274. else
  3275. {
  3276. srel.r_index[2] = r_index >> 16;
  3277. srel.r_index[1] = r_index >> 8;
  3278. srel.r_index[0] = r_index;
  3279. srel.r_type[0] =
  3280. ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
  3281. | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
  3282. | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
  3283. | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
  3284. | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
  3285. | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
  3286. }
  3287. }
  3288. #endif
  3289. rel_ptr = (void *) &srel;
  3290. /* We have to write the addend into the object file, since
  3291. standard a.out relocs are in place. It would be more
  3292. reliable if we had the current contents of the file here,
  3293. rather than assuming zeroes, but we can't read the file since
  3294. it was opened using bfd_openw. */
  3295. if (pr->addend != 0)
  3296. {
  3297. bfd_size_type size;
  3298. bfd_reloc_status_type r;
  3299. bfd_byte *buf;
  3300. bfd_boolean ok;
  3301. size = bfd_get_reloc_size (howto);
  3302. buf = (bfd_byte *) bfd_zmalloc (size);
  3303. if (buf == NULL && size != 0)
  3304. return FALSE;
  3305. r = MY_relocate_contents (howto, flaginfo->output_bfd,
  3306. (bfd_vma) pr->addend, buf);
  3307. switch (r)
  3308. {
  3309. case bfd_reloc_ok:
  3310. break;
  3311. default:
  3312. case bfd_reloc_outofrange:
  3313. abort ();
  3314. case bfd_reloc_overflow:
  3315. if (! ((*flaginfo->info->callbacks->reloc_overflow)
  3316. (flaginfo->info, NULL,
  3317. (p->type == bfd_section_reloc_link_order
  3318. ? bfd_section_name (flaginfo->output_bfd,
  3319. pr->u.section)
  3320. : pr->u.name),
  3321. howto->name, pr->addend, NULL, NULL, (bfd_vma) 0)))
  3322. {
  3323. free (buf);
  3324. return FALSE;
  3325. }
  3326. break;
  3327. }
  3328. ok = bfd_set_section_contents (flaginfo->output_bfd, o, (void *) buf,
  3329. (file_ptr) p->offset, size);
  3330. free (buf);
  3331. if (! ok)
  3332. return FALSE;
  3333. }
  3334. }
  3335. else
  3336. {
  3337. #ifdef MY_put_ext_reloc
  3338. MY_put_ext_reloc (flaginfo->output_bfd, r_extern, r_index, p->offset,
  3339. howto, &erel, pr->addend);
  3340. #else
  3341. PUT_WORD (flaginfo->output_bfd, p->offset, erel.r_address);
  3342. if (bfd_header_big_endian (flaginfo->output_bfd))
  3343. {
  3344. erel.r_index[0] = r_index >> 16;
  3345. erel.r_index[1] = r_index >> 8;
  3346. erel.r_index[2] = r_index;
  3347. erel.r_type[0] =
  3348. ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
  3349. | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
  3350. }
  3351. else
  3352. {
  3353. erel.r_index[2] = r_index >> 16;
  3354. erel.r_index[1] = r_index >> 8;
  3355. erel.r_index[0] = r_index;
  3356. erel.r_type[0] =
  3357. (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
  3358. | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
  3359. }
  3360. PUT_WORD (flaginfo->output_bfd, (bfd_vma) pr->addend, erel.r_addend);
  3361. #endif /* MY_put_ext_reloc */
  3362. rel_ptr = (void *) &erel;
  3363. }
  3364. amt = obj_reloc_entry_size (flaginfo->output_bfd);
  3365. if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
  3366. || bfd_bwrite (rel_ptr, amt, flaginfo->output_bfd) != amt)
  3367. return FALSE;
  3368. *reloff_ptr += obj_reloc_entry_size (flaginfo->output_bfd);
  3369. /* Assert that the relocs have not run into the symbols, and that n
  3370. the text relocs have not run into the data relocs. */
  3371. BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
  3372. && (reloff_ptr != &flaginfo->treloff
  3373. || (*reloff_ptr
  3374. <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
  3375. return TRUE;
  3376. }
  3377. /* Get the section corresponding to a reloc index. */
  3378. static INLINE asection *
  3379. aout_reloc_index_to_section (bfd *abfd, int indx)
  3380. {
  3381. switch (indx & N_TYPE)
  3382. {
  3383. case N_TEXT: return obj_textsec (abfd);
  3384. case N_DATA: return obj_datasec (abfd);
  3385. case N_BSS: return obj_bsssec (abfd);
  3386. case N_ABS:
  3387. case N_UNDF: return bfd_abs_section_ptr;
  3388. default: abort ();
  3389. }
  3390. return NULL;
  3391. }
  3392. /* Relocate an a.out section using standard a.out relocs. */
  3393. static bfd_boolean
  3394. aout_link_input_section_std (struct aout_final_link_info *flaginfo,
  3395. bfd *input_bfd,
  3396. asection *input_section,
  3397. struct reloc_std_external *relocs,
  3398. bfd_size_type rel_size,
  3399. bfd_byte *contents)
  3400. {
  3401. bfd_boolean (*check_dynamic_reloc)
  3402. (struct bfd_link_info *, bfd *, asection *,
  3403. struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *,
  3404. bfd_vma *);
  3405. bfd *output_bfd;
  3406. bfd_boolean relocatable;
  3407. struct external_nlist *syms;
  3408. char *strings;
  3409. struct aout_link_hash_entry **sym_hashes;
  3410. int *symbol_map;
  3411. bfd_size_type reloc_count;
  3412. struct reloc_std_external *rel;
  3413. struct reloc_std_external *rel_end;
  3414. output_bfd = flaginfo->output_bfd;
  3415. check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
  3416. BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
  3417. BFD_ASSERT (input_bfd->xvec->header_byteorder
  3418. == output_bfd->xvec->header_byteorder);
  3419. relocatable = bfd_link_relocatable (flaginfo->info);
  3420. syms = obj_aout_external_syms (input_bfd);
  3421. strings = obj_aout_external_strings (input_bfd);
  3422. sym_hashes = obj_aout_sym_hashes (input_bfd);
  3423. symbol_map = flaginfo->symbol_map;
  3424. reloc_count = rel_size / RELOC_STD_SIZE;
  3425. rel = relocs;
  3426. rel_end = rel + reloc_count;
  3427. for (; rel < rel_end; rel++)
  3428. {
  3429. bfd_vma r_addr;
  3430. int r_index;
  3431. int r_extern;
  3432. int r_pcrel;
  3433. int r_baserel = 0;
  3434. reloc_howto_type *howto;
  3435. struct aout_link_hash_entry *h = NULL;
  3436. bfd_vma relocation;
  3437. bfd_reloc_status_type r;
  3438. r_addr = GET_SWORD (input_bfd, rel->r_address);
  3439. #ifdef MY_reloc_howto
  3440. howto = MY_reloc_howto (input_bfd, rel, r_index, r_extern, r_pcrel);
  3441. #else
  3442. {
  3443. int r_jmptable;
  3444. int r_relative;
  3445. int r_length;
  3446. unsigned int howto_idx;
  3447. if (bfd_header_big_endian (input_bfd))
  3448. {
  3449. r_index = (((unsigned int) rel->r_index[0] << 16)
  3450. | ((unsigned int) rel->r_index[1] << 8)
  3451. | rel->r_index[2]);
  3452. r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
  3453. r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
  3454. r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
  3455. r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
  3456. r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
  3457. r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
  3458. >> RELOC_STD_BITS_LENGTH_SH_BIG);
  3459. }
  3460. else
  3461. {
  3462. r_index = (((unsigned int) rel->r_index[2] << 16)
  3463. | ((unsigned int) rel->r_index[1] << 8)
  3464. | rel->r_index[0]);
  3465. r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
  3466. r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
  3467. r_baserel = (0 != (rel->r_type[0]
  3468. & RELOC_STD_BITS_BASEREL_LITTLE));
  3469. r_jmptable= (0 != (rel->r_type[0]
  3470. & RELOC_STD_BITS_JMPTABLE_LITTLE));
  3471. r_relative= (0 != (rel->r_type[0]
  3472. & RELOC_STD_BITS_RELATIVE_LITTLE));
  3473. r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
  3474. >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
  3475. }
  3476. howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
  3477. + 16 * r_jmptable + 32 * r_relative);
  3478. if (howto_idx < TABLE_SIZE (howto_table_std))
  3479. howto = howto_table_std + howto_idx;
  3480. else
  3481. howto = NULL;
  3482. }
  3483. #endif
  3484. if (howto == NULL)
  3485. {
  3486. (*flaginfo->info->callbacks->einfo)
  3487. (_("%P: %B: unexpected relocation type\n"), input_bfd);
  3488. bfd_set_error (bfd_error_bad_value);
  3489. return FALSE;
  3490. }
  3491. if (relocatable)
  3492. {
  3493. /* We are generating a relocatable output file, and must
  3494. modify the reloc accordingly. */
  3495. if (r_extern)
  3496. {
  3497. /* If we know the symbol this relocation is against,
  3498. convert it into a relocation against a section. This
  3499. is what the native linker does. */
  3500. h = sym_hashes[r_index];
  3501. if (h != NULL
  3502. && (h->root.type == bfd_link_hash_defined
  3503. || h->root.type == bfd_link_hash_defweak))
  3504. {
  3505. asection *output_section;
  3506. /* Change the r_extern value. */
  3507. if (bfd_header_big_endian (output_bfd))
  3508. rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
  3509. else
  3510. rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
  3511. /* Compute a new r_index. */
  3512. output_section = h->root.u.def.section->output_section;
  3513. if (output_section == obj_textsec (output_bfd))
  3514. r_index = N_TEXT;
  3515. else if (output_section == obj_datasec (output_bfd))
  3516. r_index = N_DATA;
  3517. else if (output_section == obj_bsssec (output_bfd))
  3518. r_index = N_BSS;
  3519. else
  3520. r_index = N_ABS;
  3521. /* Add the symbol value and the section VMA to the
  3522. addend stored in the contents. */
  3523. relocation = (h->root.u.def.value
  3524. + output_section->vma
  3525. + h->root.u.def.section->output_offset);
  3526. }
  3527. else
  3528. {
  3529. /* We must change r_index according to the symbol
  3530. map. */
  3531. r_index = symbol_map[r_index];
  3532. if (r_index == -1)
  3533. {
  3534. if (h != NULL)
  3535. {
  3536. /* We decided to strip this symbol, but it
  3537. turns out that we can't. Note that we
  3538. lose the other and desc information here.
  3539. I don't think that will ever matter for a
  3540. global symbol. */
  3541. if (h->indx < 0)
  3542. {
  3543. h->indx = -2;
  3544. h->written = FALSE;
  3545. if (!aout_link_write_other_symbol (&h->root.root,
  3546. flaginfo))
  3547. return FALSE;
  3548. }
  3549. r_index = h->indx;
  3550. }
  3551. else
  3552. {
  3553. const char *name;
  3554. name = strings + GET_WORD (input_bfd,
  3555. syms[r_index].e_strx);
  3556. if (! ((*flaginfo->info->callbacks->unattached_reloc)
  3557. (flaginfo->info, name, input_bfd, input_section,
  3558. r_addr)))
  3559. return FALSE;
  3560. r_index = 0;
  3561. }
  3562. }
  3563. relocation = 0;
  3564. }
  3565. /* Write out the new r_index value. */
  3566. if (bfd_header_big_endian (output_bfd))
  3567. {
  3568. rel->r_index[0] = r_index >> 16;
  3569. rel->r_index[1] = r_index >> 8;
  3570. rel->r_index[2] = r_index;
  3571. }
  3572. else
  3573. {
  3574. rel->r_index[2] = r_index >> 16;
  3575. rel->r_index[1] = r_index >> 8;
  3576. rel->r_index[0] = r_index;
  3577. }
  3578. }
  3579. else
  3580. {
  3581. asection *section;
  3582. /* This is a relocation against a section. We must
  3583. adjust by the amount that the section moved. */
  3584. section = aout_reloc_index_to_section (input_bfd, r_index);
  3585. relocation = (section->output_section->vma
  3586. + section->output_offset
  3587. - section->vma);
  3588. }
  3589. /* Change the address of the relocation. */
  3590. PUT_WORD (output_bfd,
  3591. r_addr + input_section->output_offset,
  3592. rel->r_address);
  3593. /* Adjust a PC relative relocation by removing the reference
  3594. to the original address in the section and including the
  3595. reference to the new address. */
  3596. if (r_pcrel)
  3597. relocation -= (input_section->output_section->vma
  3598. + input_section->output_offset
  3599. - input_section->vma);
  3600. #ifdef MY_relocatable_reloc
  3601. MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
  3602. #endif
  3603. if (relocation == 0)
  3604. r = bfd_reloc_ok;
  3605. else
  3606. r = MY_relocate_contents (howto,
  3607. input_bfd, relocation,
  3608. contents + r_addr);
  3609. }
  3610. else
  3611. {
  3612. bfd_boolean hundef;
  3613. /* We are generating an executable, and must do a full
  3614. relocation. */
  3615. hundef = FALSE;
  3616. if (r_extern)
  3617. {
  3618. h = sym_hashes[r_index];
  3619. if (h != NULL
  3620. && (h->root.type == bfd_link_hash_defined
  3621. || h->root.type == bfd_link_hash_defweak))
  3622. {
  3623. relocation = (h->root.u.def.value
  3624. + h->root.u.def.section->output_section->vma
  3625. + h->root.u.def.section->output_offset);
  3626. }
  3627. else if (h != NULL
  3628. && h->root.type == bfd_link_hash_undefweak)
  3629. relocation = 0;
  3630. else
  3631. {
  3632. hundef = TRUE;
  3633. relocation = 0;
  3634. }
  3635. }
  3636. else
  3637. {
  3638. asection *section;
  3639. section = aout_reloc_index_to_section (input_bfd, r_index);
  3640. relocation = (section->output_section->vma
  3641. + section->output_offset
  3642. - section->vma);
  3643. if (r_pcrel)
  3644. relocation += input_section->vma;
  3645. }
  3646. if (check_dynamic_reloc != NULL)
  3647. {
  3648. bfd_boolean skip;
  3649. if (! ((*check_dynamic_reloc)
  3650. (flaginfo->info, input_bfd, input_section, h,
  3651. (void *) rel, contents, &skip, &relocation)))
  3652. return FALSE;
  3653. if (skip)
  3654. continue;
  3655. }
  3656. /* Now warn if a global symbol is undefined. We could not
  3657. do this earlier, because check_dynamic_reloc might want
  3658. to skip this reloc. */
  3659. if (hundef && ! bfd_link_pic (flaginfo->info) && ! r_baserel)
  3660. {
  3661. const char *name;
  3662. if (h != NULL)
  3663. name = h->root.root.string;
  3664. else
  3665. name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
  3666. if (! ((*flaginfo->info->callbacks->undefined_symbol)
  3667. (flaginfo->info, name, input_bfd, input_section,
  3668. r_addr, TRUE)))
  3669. return FALSE;
  3670. }
  3671. r = MY_final_link_relocate (howto,
  3672. input_bfd, input_section,
  3673. contents, r_addr, relocation,
  3674. (bfd_vma) 0);
  3675. }
  3676. if (r != bfd_reloc_ok)
  3677. {
  3678. switch (r)
  3679. {
  3680. default:
  3681. case bfd_reloc_outofrange:
  3682. abort ();
  3683. case bfd_reloc_overflow:
  3684. {
  3685. const char *name;
  3686. if (h != NULL)
  3687. name = NULL;
  3688. else if (r_extern)
  3689. name = strings + GET_WORD (input_bfd,
  3690. syms[r_index].e_strx);
  3691. else
  3692. {
  3693. asection *s;
  3694. s = aout_reloc_index_to_section (input_bfd, r_index);
  3695. name = bfd_section_name (input_bfd, s);
  3696. }
  3697. if (! ((*flaginfo->info->callbacks->reloc_overflow)
  3698. (flaginfo->info, (h ? &h->root : NULL), name,
  3699. howto->name, (bfd_vma) 0, input_bfd,
  3700. input_section, r_addr)))
  3701. return FALSE;
  3702. }
  3703. break;
  3704. }
  3705. }
  3706. }
  3707. return TRUE;
  3708. }
  3709. /* Relocate an a.out section using extended a.out relocs. */
  3710. static bfd_boolean
  3711. aout_link_input_section_ext (struct aout_final_link_info *flaginfo,
  3712. bfd *input_bfd,
  3713. asection *input_section,
  3714. struct reloc_ext_external *relocs,
  3715. bfd_size_type rel_size,
  3716. bfd_byte *contents)
  3717. {
  3718. bfd_boolean (*check_dynamic_reloc)
  3719. (struct bfd_link_info *, bfd *, asection *,
  3720. struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *,
  3721. bfd_vma *);
  3722. bfd *output_bfd;
  3723. bfd_boolean relocatable;
  3724. struct external_nlist *syms;
  3725. char *strings;
  3726. struct aout_link_hash_entry **sym_hashes;
  3727. int *symbol_map;
  3728. bfd_size_type reloc_count;
  3729. struct reloc_ext_external *rel;
  3730. struct reloc_ext_external *rel_end;
  3731. output_bfd = flaginfo->output_bfd;
  3732. check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
  3733. BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
  3734. BFD_ASSERT (input_bfd->xvec->header_byteorder
  3735. == output_bfd->xvec->header_byteorder);
  3736. relocatable = bfd_link_relocatable (flaginfo->info);
  3737. syms = obj_aout_external_syms (input_bfd);
  3738. strings = obj_aout_external_strings (input_bfd);
  3739. sym_hashes = obj_aout_sym_hashes (input_bfd);
  3740. symbol_map = flaginfo->symbol_map;
  3741. reloc_count = rel_size / RELOC_EXT_SIZE;
  3742. rel = relocs;
  3743. rel_end = rel + reloc_count;
  3744. for (; rel < rel_end; rel++)
  3745. {
  3746. bfd_vma r_addr;
  3747. int r_index;
  3748. int r_extern;
  3749. unsigned int r_type;
  3750. bfd_vma r_addend;
  3751. struct aout_link_hash_entry *h = NULL;
  3752. asection *r_section = NULL;
  3753. bfd_vma relocation;
  3754. r_addr = GET_SWORD (input_bfd, rel->r_address);
  3755. if (bfd_header_big_endian (input_bfd))
  3756. {
  3757. r_index = (((unsigned int) rel->r_index[0] << 16)
  3758. | ((unsigned int) rel->r_index[1] << 8)
  3759. | rel->r_index[2]);
  3760. r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
  3761. r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
  3762. >> RELOC_EXT_BITS_TYPE_SH_BIG);
  3763. }
  3764. else
  3765. {
  3766. r_index = (((unsigned int) rel->r_index[2] << 16)
  3767. | ((unsigned int) rel->r_index[1] << 8)
  3768. | rel->r_index[0]);
  3769. r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
  3770. r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
  3771. >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
  3772. }
  3773. r_addend = GET_SWORD (input_bfd, rel->r_addend);
  3774. if (r_type >= TABLE_SIZE (howto_table_ext))
  3775. {
  3776. (*flaginfo->info->callbacks->einfo)
  3777. (_("%P: %B: unexpected relocation type\n"), input_bfd);
  3778. bfd_set_error (bfd_error_bad_value);
  3779. return FALSE;
  3780. }
  3781. if (relocatable)
  3782. {
  3783. /* We are generating a relocatable output file, and must
  3784. modify the reloc accordingly. */
  3785. if (r_extern
  3786. || r_type == (unsigned int) RELOC_BASE10
  3787. || r_type == (unsigned int) RELOC_BASE13
  3788. || r_type == (unsigned int) RELOC_BASE22)
  3789. {
  3790. /* If we know the symbol this relocation is against,
  3791. convert it into a relocation against a section. This
  3792. is what the native linker does. */
  3793. if (r_type == (unsigned int) RELOC_BASE10
  3794. || r_type == (unsigned int) RELOC_BASE13
  3795. || r_type == (unsigned int) RELOC_BASE22)
  3796. h = NULL;
  3797. else
  3798. h = sym_hashes[r_index];
  3799. if (h != NULL
  3800. && (h->root.type == bfd_link_hash_defined
  3801. || h->root.type == bfd_link_hash_defweak))
  3802. {
  3803. asection *output_section;
  3804. /* Change the r_extern value. */
  3805. if (bfd_header_big_endian (output_bfd))
  3806. rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
  3807. else
  3808. rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
  3809. /* Compute a new r_index. */
  3810. output_section = h->root.u.def.section->output_section;
  3811. if (output_section == obj_textsec (output_bfd))
  3812. r_index = N_TEXT;
  3813. else if (output_section == obj_datasec (output_bfd))
  3814. r_index = N_DATA;
  3815. else if (output_section == obj_bsssec (output_bfd))
  3816. r_index = N_BSS;
  3817. else
  3818. r_index = N_ABS;
  3819. /* Add the symbol value and the section VMA to the
  3820. addend. */
  3821. relocation = (h->root.u.def.value
  3822. + output_section->vma
  3823. + h->root.u.def.section->output_offset);
  3824. /* Now RELOCATION is the VMA of the final
  3825. destination. If this is a PC relative reloc,
  3826. then ADDEND is the negative of the source VMA.
  3827. We want to set ADDEND to the difference between
  3828. the destination VMA and the source VMA, which
  3829. means we must adjust RELOCATION by the change in
  3830. the source VMA. This is done below. */
  3831. }
  3832. else
  3833. {
  3834. /* We must change r_index according to the symbol
  3835. map. */
  3836. r_index = symbol_map[r_index];
  3837. if (r_index == -1)
  3838. {
  3839. if (h != NULL)
  3840. {
  3841. /* We decided to strip this symbol, but it
  3842. turns out that we can't. Note that we
  3843. lose the other and desc information here.
  3844. I don't think that will ever matter for a
  3845. global symbol. */
  3846. if (h->indx < 0)
  3847. {
  3848. h->indx = -2;
  3849. h->written = FALSE;
  3850. if (!aout_link_write_other_symbol (&h->root.root,
  3851. flaginfo))
  3852. return FALSE;
  3853. }
  3854. r_index = h->indx;
  3855. }
  3856. else
  3857. {
  3858. const char *name;
  3859. name = strings + GET_WORD (input_bfd,
  3860. syms[r_index].e_strx);
  3861. if (! ((*flaginfo->info->callbacks->unattached_reloc)
  3862. (flaginfo->info, name, input_bfd, input_section,
  3863. r_addr)))
  3864. return FALSE;
  3865. r_index = 0;
  3866. }
  3867. }
  3868. relocation = 0;
  3869. /* If this is a PC relative reloc, then the addend
  3870. is the negative of the source VMA. We must
  3871. adjust it by the change in the source VMA. This
  3872. is done below. */
  3873. }
  3874. /* Write out the new r_index value. */
  3875. if (bfd_header_big_endian (output_bfd))
  3876. {
  3877. rel->r_index[0] = r_index >> 16;
  3878. rel->r_index[1] = r_index >> 8;
  3879. rel->r_index[2] = r_index;
  3880. }
  3881. else
  3882. {
  3883. rel->r_index[2] = r_index >> 16;
  3884. rel->r_index[1] = r_index >> 8;
  3885. rel->r_index[0] = r_index;
  3886. }
  3887. }
  3888. else
  3889. {
  3890. /* This is a relocation against a section. We must
  3891. adjust by the amount that the section moved. */
  3892. r_section = aout_reloc_index_to_section (input_bfd, r_index);
  3893. relocation = (r_section->output_section->vma
  3894. + r_section->output_offset
  3895. - r_section->vma);
  3896. /* If this is a PC relative reloc, then the addend is
  3897. the difference in VMA between the destination and the
  3898. source. We have just adjusted for the change in VMA
  3899. of the destination, so we must also adjust by the
  3900. change in VMA of the source. This is done below. */
  3901. }
  3902. /* As described above, we must always adjust a PC relative
  3903. reloc by the change in VMA of the source. However, if
  3904. pcrel_offset is set, then the addend does not include the
  3905. location within the section, in which case we don't need
  3906. to adjust anything. */
  3907. if (howto_table_ext[r_type].pc_relative
  3908. && ! howto_table_ext[r_type].pcrel_offset)
  3909. relocation -= (input_section->output_section->vma
  3910. + input_section->output_offset
  3911. - input_section->vma);
  3912. /* Change the addend if necessary. */
  3913. if (relocation != 0)
  3914. PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
  3915. /* Change the address of the relocation. */
  3916. PUT_WORD (output_bfd,
  3917. r_addr + input_section->output_offset,
  3918. rel->r_address);
  3919. }
  3920. else
  3921. {
  3922. bfd_boolean hundef;
  3923. bfd_reloc_status_type r;
  3924. /* We are generating an executable, and must do a full
  3925. relocation. */
  3926. hundef = FALSE;
  3927. if (r_extern)
  3928. {
  3929. h = sym_hashes[r_index];
  3930. if (h != NULL
  3931. && (h->root.type == bfd_link_hash_defined
  3932. || h->root.type == bfd_link_hash_defweak))
  3933. {
  3934. relocation = (h->root.u.def.value
  3935. + h->root.u.def.section->output_section->vma
  3936. + h->root.u.def.section->output_offset);
  3937. }
  3938. else if (h != NULL
  3939. && h->root.type == bfd_link_hash_undefweak)
  3940. relocation = 0;
  3941. else
  3942. {
  3943. hundef = TRUE;
  3944. relocation = 0;
  3945. }
  3946. }
  3947. else if (r_type == (unsigned int) RELOC_BASE10
  3948. || r_type == (unsigned int) RELOC_BASE13
  3949. || r_type == (unsigned int) RELOC_BASE22)
  3950. {
  3951. struct external_nlist *sym;
  3952. int type;
  3953. /* For base relative relocs, r_index is always an index
  3954. into the symbol table, even if r_extern is 0. */
  3955. sym = syms + r_index;
  3956. type = H_GET_8 (input_bfd, sym->e_type);
  3957. if ((type & N_TYPE) == N_TEXT
  3958. || type == N_WEAKT)
  3959. r_section = obj_textsec (input_bfd);
  3960. else if ((type & N_TYPE) == N_DATA
  3961. || type == N_WEAKD)
  3962. r_section = obj_datasec (input_bfd);
  3963. else if ((type & N_TYPE) == N_BSS
  3964. || type == N_WEAKB)
  3965. r_section = obj_bsssec (input_bfd);
  3966. else if ((type & N_TYPE) == N_ABS
  3967. || type == N_WEAKA)
  3968. r_section = bfd_abs_section_ptr;
  3969. else
  3970. abort ();
  3971. relocation = (r_section->output_section->vma
  3972. + r_section->output_offset
  3973. + (GET_WORD (input_bfd, sym->e_value)
  3974. - r_section->vma));
  3975. }
  3976. else
  3977. {
  3978. r_section = aout_reloc_index_to_section (input_bfd, r_index);
  3979. /* If this is a PC relative reloc, then R_ADDEND is the
  3980. difference between the two vmas, or
  3981. old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
  3982. where
  3983. old_dest_sec == section->vma
  3984. and
  3985. old_src_sec == input_section->vma
  3986. and
  3987. old_src_off == r_addr
  3988. _bfd_final_link_relocate expects RELOCATION +
  3989. R_ADDEND to be the VMA of the destination minus
  3990. r_addr (the minus r_addr is because this relocation
  3991. is not pcrel_offset, which is a bit confusing and
  3992. should, perhaps, be changed), or
  3993. new_dest_sec
  3994. where
  3995. new_dest_sec == output_section->vma + output_offset
  3996. We arrange for this to happen by setting RELOCATION to
  3997. new_dest_sec + old_src_sec - old_dest_sec
  3998. If this is not a PC relative reloc, then R_ADDEND is
  3999. simply the VMA of the destination, so we set
  4000. RELOCATION to the change in the destination VMA, or
  4001. new_dest_sec - old_dest_sec
  4002. */
  4003. relocation = (r_section->output_section->vma
  4004. + r_section->output_offset
  4005. - r_section->vma);
  4006. if (howto_table_ext[r_type].pc_relative)
  4007. relocation += input_section->vma;
  4008. }
  4009. if (check_dynamic_reloc != NULL)
  4010. {
  4011. bfd_boolean skip;
  4012. if (! ((*check_dynamic_reloc)
  4013. (flaginfo->info, input_bfd, input_section, h,
  4014. (void *) rel, contents, &skip, &relocation)))
  4015. return FALSE;
  4016. if (skip)
  4017. continue;
  4018. }
  4019. /* Now warn if a global symbol is undefined. We could not
  4020. do this earlier, because check_dynamic_reloc might want
  4021. to skip this reloc. */
  4022. if (hundef
  4023. && ! bfd_link_pic (flaginfo->info)
  4024. && r_type != (unsigned int) RELOC_BASE10
  4025. && r_type != (unsigned int) RELOC_BASE13
  4026. && r_type != (unsigned int) RELOC_BASE22)
  4027. {
  4028. const char *name;
  4029. if (h != NULL)
  4030. name = h->root.root.string;
  4031. else
  4032. name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
  4033. if (! ((*flaginfo->info->callbacks->undefined_symbol)
  4034. (flaginfo->info, name, input_bfd, input_section,
  4035. r_addr, TRUE)))
  4036. return FALSE;
  4037. }
  4038. if (r_type != (unsigned int) RELOC_SPARC_REV32)
  4039. r = MY_final_link_relocate (howto_table_ext + r_type,
  4040. input_bfd, input_section,
  4041. contents, r_addr, relocation,
  4042. r_addend);
  4043. else
  4044. {
  4045. bfd_vma x;
  4046. x = bfd_get_32 (input_bfd, contents + r_addr);
  4047. x = x + relocation + r_addend;
  4048. bfd_putl32 (/*input_bfd,*/ x, contents + r_addr);
  4049. r = bfd_reloc_ok;
  4050. }
  4051. if (r != bfd_reloc_ok)
  4052. {
  4053. switch (r)
  4054. {
  4055. default:
  4056. case bfd_reloc_outofrange:
  4057. abort ();
  4058. case bfd_reloc_overflow:
  4059. {
  4060. const char *name;
  4061. if (h != NULL)
  4062. name = NULL;
  4063. else if (r_extern
  4064. || r_type == (unsigned int) RELOC_BASE10
  4065. || r_type == (unsigned int) RELOC_BASE13
  4066. || r_type == (unsigned int) RELOC_BASE22)
  4067. name = strings + GET_WORD (input_bfd,
  4068. syms[r_index].e_strx);
  4069. else
  4070. {
  4071. asection *s;
  4072. s = aout_reloc_index_to_section (input_bfd, r_index);
  4073. name = bfd_section_name (input_bfd, s);
  4074. }
  4075. if (! ((*flaginfo->info->callbacks->reloc_overflow)
  4076. (flaginfo->info, (h ? &h->root : NULL), name,
  4077. howto_table_ext[r_type].name,
  4078. r_addend, input_bfd, input_section, r_addr)))
  4079. return FALSE;
  4080. }
  4081. break;
  4082. }
  4083. }
  4084. }
  4085. }
  4086. return TRUE;
  4087. }
  4088. /* Link an a.out section into the output file. */
  4089. static bfd_boolean
  4090. aout_link_input_section (struct aout_final_link_info *flaginfo,
  4091. bfd *input_bfd,
  4092. asection *input_section,
  4093. file_ptr *reloff_ptr,
  4094. bfd_size_type rel_size)
  4095. {
  4096. bfd_size_type input_size;
  4097. void * relocs;
  4098. /* Get the section contents. */
  4099. input_size = input_section->size;
  4100. if (! bfd_get_section_contents (input_bfd, input_section,
  4101. (void *) flaginfo->contents,
  4102. (file_ptr) 0, input_size))
  4103. return FALSE;
  4104. /* Read in the relocs if we haven't already done it. */
  4105. if (aout_section_data (input_section) != NULL
  4106. && aout_section_data (input_section)->relocs != NULL)
  4107. relocs = aout_section_data (input_section)->relocs;
  4108. else
  4109. {
  4110. relocs = flaginfo->relocs;
  4111. if (rel_size > 0)
  4112. {
  4113. if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
  4114. || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
  4115. return FALSE;
  4116. }
  4117. }
  4118. /* Relocate the section contents. */
  4119. if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
  4120. {
  4121. if (! aout_link_input_section_std (flaginfo, input_bfd, input_section,
  4122. (struct reloc_std_external *) relocs,
  4123. rel_size, flaginfo->contents))
  4124. return FALSE;
  4125. }
  4126. else
  4127. {
  4128. if (! aout_link_input_section_ext (flaginfo, input_bfd, input_section,
  4129. (struct reloc_ext_external *) relocs,
  4130. rel_size, flaginfo->contents))
  4131. return FALSE;
  4132. }
  4133. /* Write out the section contents. */
  4134. if (! bfd_set_section_contents (flaginfo->output_bfd,
  4135. input_section->output_section,
  4136. (void *) flaginfo->contents,
  4137. (file_ptr) input_section->output_offset,
  4138. input_size))
  4139. return FALSE;
  4140. /* If we are producing relocatable output, the relocs were
  4141. modified, and we now write them out. */
  4142. if (bfd_link_relocatable (flaginfo->info) && rel_size > 0)
  4143. {
  4144. if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
  4145. return FALSE;
  4146. if (bfd_bwrite (relocs, rel_size, flaginfo->output_bfd) != rel_size)
  4147. return FALSE;
  4148. *reloff_ptr += rel_size;
  4149. /* Assert that the relocs have not run into the symbols, and
  4150. that if these are the text relocs they have not run into the
  4151. data relocs. */
  4152. BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
  4153. && (reloff_ptr != &flaginfo->treloff
  4154. || (*reloff_ptr
  4155. <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
  4156. }
  4157. return TRUE;
  4158. }
  4159. /* Adjust and write out the symbols for an a.out file. Set the new
  4160. symbol indices into a symbol_map. */
  4161. static bfd_boolean
  4162. aout_link_write_symbols (struct aout_final_link_info *flaginfo, bfd *input_bfd)
  4163. {
  4164. bfd *output_bfd;
  4165. bfd_size_type sym_count;
  4166. char *strings;
  4167. enum bfd_link_strip strip;
  4168. enum bfd_link_discard discard;
  4169. struct external_nlist *outsym;
  4170. bfd_size_type strtab_index;
  4171. struct external_nlist *sym;
  4172. struct external_nlist *sym_end;
  4173. struct aout_link_hash_entry **sym_hash;
  4174. int *symbol_map;
  4175. bfd_boolean pass;
  4176. bfd_boolean skip_next;
  4177. output_bfd = flaginfo->output_bfd;
  4178. sym_count = obj_aout_external_sym_count (input_bfd);
  4179. strings = obj_aout_external_strings (input_bfd);
  4180. strip = flaginfo->info->strip;
  4181. discard = flaginfo->info->discard;
  4182. outsym = flaginfo->output_syms;
  4183. /* First write out a symbol for this object file, unless we are
  4184. discarding such symbols. */
  4185. if (strip != strip_all
  4186. && (strip != strip_some
  4187. || bfd_hash_lookup (flaginfo->info->keep_hash, input_bfd->filename,
  4188. FALSE, FALSE) != NULL)
  4189. && discard != discard_all)
  4190. {
  4191. H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
  4192. H_PUT_8 (output_bfd, 0, outsym->e_other);
  4193. H_PUT_16 (output_bfd, 0, outsym->e_desc);
  4194. strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
  4195. input_bfd->filename, FALSE);
  4196. if (strtab_index == (bfd_size_type) -1)
  4197. return FALSE;
  4198. PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
  4199. PUT_WORD (output_bfd,
  4200. (bfd_get_section_vma (output_bfd,
  4201. obj_textsec (input_bfd)->output_section)
  4202. + obj_textsec (input_bfd)->output_offset),
  4203. outsym->e_value);
  4204. ++obj_aout_external_sym_count (output_bfd);
  4205. ++outsym;
  4206. }
  4207. pass = FALSE;
  4208. skip_next = FALSE;
  4209. sym = obj_aout_external_syms (input_bfd);
  4210. sym_end = sym + sym_count;
  4211. sym_hash = obj_aout_sym_hashes (input_bfd);
  4212. symbol_map = flaginfo->symbol_map;
  4213. memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
  4214. for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
  4215. {
  4216. const char *name;
  4217. int type;
  4218. struct aout_link_hash_entry *h;
  4219. bfd_boolean skip;
  4220. asection *symsec;
  4221. bfd_vma val = 0;
  4222. bfd_boolean copy;
  4223. /* We set *symbol_map to 0 above for all symbols. If it has
  4224. already been set to -1 for this symbol, it means that we are
  4225. discarding it because it appears in a duplicate header file.
  4226. See the N_BINCL code below. */
  4227. if (*symbol_map == -1)
  4228. continue;
  4229. /* Initialize *symbol_map to -1, which means that the symbol was
  4230. not copied into the output file. We will change it later if
  4231. we do copy the symbol over. */
  4232. *symbol_map = -1;
  4233. type = H_GET_8 (input_bfd, sym->e_type);
  4234. name = strings + GET_WORD (input_bfd, sym->e_strx);
  4235. h = NULL;
  4236. if (pass)
  4237. {
  4238. /* Pass this symbol through. It is the target of an
  4239. indirect or warning symbol. */
  4240. val = GET_WORD (input_bfd, sym->e_value);
  4241. pass = FALSE;
  4242. }
  4243. else if (skip_next)
  4244. {
  4245. /* Skip this symbol, which is the target of an indirect
  4246. symbol that we have changed to no longer be an indirect
  4247. symbol. */
  4248. skip_next = FALSE;
  4249. continue;
  4250. }
  4251. else
  4252. {
  4253. struct aout_link_hash_entry *hresolve;
  4254. /* We have saved the hash table entry for this symbol, if
  4255. there is one. Note that we could just look it up again
  4256. in the hash table, provided we first check that it is an
  4257. external symbol. */
  4258. h = *sym_hash;
  4259. /* Use the name from the hash table, in case the symbol was
  4260. wrapped. */
  4261. if (h != NULL
  4262. && h->root.type != bfd_link_hash_warning)
  4263. name = h->root.root.string;
  4264. /* If this is an indirect or warning symbol, then change
  4265. hresolve to the base symbol. We also change *sym_hash so
  4266. that the relocation routines relocate against the real
  4267. symbol. */
  4268. hresolve = h;
  4269. if (h != (struct aout_link_hash_entry *) NULL
  4270. && (h->root.type == bfd_link_hash_indirect
  4271. || h->root.type == bfd_link_hash_warning))
  4272. {
  4273. hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
  4274. while (hresolve->root.type == bfd_link_hash_indirect
  4275. || hresolve->root.type == bfd_link_hash_warning)
  4276. hresolve = ((struct aout_link_hash_entry *)
  4277. hresolve->root.u.i.link);
  4278. *sym_hash = hresolve;
  4279. }
  4280. /* If the symbol has already been written out, skip it. */
  4281. if (h != NULL
  4282. && h->written)
  4283. {
  4284. if ((type & N_TYPE) == N_INDR
  4285. || type == N_WARNING)
  4286. skip_next = TRUE;
  4287. *symbol_map = h->indx;
  4288. continue;
  4289. }
  4290. /* See if we are stripping this symbol. */
  4291. skip = FALSE;
  4292. switch (strip)
  4293. {
  4294. case strip_none:
  4295. break;
  4296. case strip_debugger:
  4297. if ((type & N_STAB) != 0)
  4298. skip = TRUE;
  4299. break;
  4300. case strip_some:
  4301. if (bfd_hash_lookup (flaginfo->info->keep_hash, name, FALSE, FALSE)
  4302. == NULL)
  4303. skip = TRUE;
  4304. break;
  4305. case strip_all:
  4306. skip = TRUE;
  4307. break;
  4308. }
  4309. if (skip)
  4310. {
  4311. if (h != NULL)
  4312. h->written = TRUE;
  4313. continue;
  4314. }
  4315. /* Get the value of the symbol. */
  4316. if ((type & N_TYPE) == N_TEXT
  4317. || type == N_WEAKT)
  4318. symsec = obj_textsec (input_bfd);
  4319. else if ((type & N_TYPE) == N_DATA
  4320. || type == N_WEAKD)
  4321. symsec = obj_datasec (input_bfd);
  4322. else if ((type & N_TYPE) == N_BSS
  4323. || type == N_WEAKB)
  4324. symsec = obj_bsssec (input_bfd);
  4325. else if ((type & N_TYPE) == N_ABS
  4326. || type == N_WEAKA)
  4327. symsec = bfd_abs_section_ptr;
  4328. else if (((type & N_TYPE) == N_INDR
  4329. && (hresolve == NULL
  4330. || (hresolve->root.type != bfd_link_hash_defined
  4331. && hresolve->root.type != bfd_link_hash_defweak
  4332. && hresolve->root.type != bfd_link_hash_common)))
  4333. || type == N_WARNING)
  4334. {
  4335. /* Pass the next symbol through unchanged. The
  4336. condition above for indirect symbols is so that if
  4337. the indirect symbol was defined, we output it with
  4338. the correct definition so the debugger will
  4339. understand it. */
  4340. pass = TRUE;
  4341. val = GET_WORD (input_bfd, sym->e_value);
  4342. symsec = NULL;
  4343. }
  4344. else if ((type & N_STAB) != 0)
  4345. {
  4346. val = GET_WORD (input_bfd, sym->e_value);
  4347. symsec = NULL;
  4348. }
  4349. else
  4350. {
  4351. /* If we get here with an indirect symbol, it means that
  4352. we are outputting it with a real definition. In such
  4353. a case we do not want to output the next symbol,
  4354. which is the target of the indirection. */
  4355. if ((type & N_TYPE) == N_INDR)
  4356. skip_next = TRUE;
  4357. symsec = NULL;
  4358. /* We need to get the value from the hash table. We use
  4359. hresolve so that if we have defined an indirect
  4360. symbol we output the final definition. */
  4361. if (h == NULL)
  4362. {
  4363. switch (type & N_TYPE)
  4364. {
  4365. case N_SETT:
  4366. symsec = obj_textsec (input_bfd);
  4367. break;
  4368. case N_SETD:
  4369. symsec = obj_datasec (input_bfd);
  4370. break;
  4371. case N_SETB:
  4372. symsec = obj_bsssec (input_bfd);
  4373. break;
  4374. case N_SETA:
  4375. symsec = bfd_abs_section_ptr;
  4376. break;
  4377. default:
  4378. val = 0;
  4379. break;
  4380. }
  4381. }
  4382. else if (hresolve->root.type == bfd_link_hash_defined
  4383. || hresolve->root.type == bfd_link_hash_defweak)
  4384. {
  4385. asection *input_section;
  4386. asection *output_section;
  4387. /* This case usually means a common symbol which was
  4388. turned into a defined symbol. */
  4389. input_section = hresolve->root.u.def.section;
  4390. output_section = input_section->output_section;
  4391. BFD_ASSERT (bfd_is_abs_section (output_section)
  4392. || output_section->owner == output_bfd);
  4393. val = (hresolve->root.u.def.value
  4394. + bfd_get_section_vma (output_bfd, output_section)
  4395. + input_section->output_offset);
  4396. /* Get the correct type based on the section. If
  4397. this is a constructed set, force it to be
  4398. globally visible. */
  4399. if (type == N_SETT
  4400. || type == N_SETD
  4401. || type == N_SETB
  4402. || type == N_SETA)
  4403. type |= N_EXT;
  4404. type &=~ N_TYPE;
  4405. if (output_section == obj_textsec (output_bfd))
  4406. type |= (hresolve->root.type == bfd_link_hash_defined
  4407. ? N_TEXT
  4408. : N_WEAKT);
  4409. else if (output_section == obj_datasec (output_bfd))
  4410. type |= (hresolve->root.type == bfd_link_hash_defined
  4411. ? N_DATA
  4412. : N_WEAKD);
  4413. else if (output_section == obj_bsssec (output_bfd))
  4414. type |= (hresolve->root.type == bfd_link_hash_defined
  4415. ? N_BSS
  4416. : N_WEAKB);
  4417. else
  4418. type |= (hresolve->root.type == bfd_link_hash_defined
  4419. ? N_ABS
  4420. : N_WEAKA);
  4421. }
  4422. else if (hresolve->root.type == bfd_link_hash_common)
  4423. val = hresolve->root.u.c.size;
  4424. else if (hresolve->root.type == bfd_link_hash_undefweak)
  4425. {
  4426. val = 0;
  4427. type = N_WEAKU;
  4428. }
  4429. else
  4430. val = 0;
  4431. }
  4432. if (symsec != NULL)
  4433. val = (symsec->output_section->vma
  4434. + symsec->output_offset
  4435. + (GET_WORD (input_bfd, sym->e_value)
  4436. - symsec->vma));
  4437. /* If this is a global symbol set the written flag, and if
  4438. it is a local symbol see if we should discard it. */
  4439. if (h != NULL)
  4440. {
  4441. h->written = TRUE;
  4442. h->indx = obj_aout_external_sym_count (output_bfd);
  4443. }
  4444. else if ((type & N_TYPE) != N_SETT
  4445. && (type & N_TYPE) != N_SETD
  4446. && (type & N_TYPE) != N_SETB
  4447. && (type & N_TYPE) != N_SETA)
  4448. {
  4449. switch (discard)
  4450. {
  4451. case discard_none:
  4452. case discard_sec_merge:
  4453. break;
  4454. case discard_l:
  4455. if ((type & N_STAB) == 0
  4456. && bfd_is_local_label_name (input_bfd, name))
  4457. skip = TRUE;
  4458. break;
  4459. case discard_all:
  4460. skip = TRUE;
  4461. break;
  4462. }
  4463. if (skip)
  4464. {
  4465. pass = FALSE;
  4466. continue;
  4467. }
  4468. }
  4469. /* An N_BINCL symbol indicates the start of the stabs
  4470. entries for a header file. We need to scan ahead to the
  4471. next N_EINCL symbol, ignoring nesting, adding up all the
  4472. characters in the symbol names, not including the file
  4473. numbers in types (the first number after an open
  4474. parenthesis). */
  4475. if (type == (int) N_BINCL)
  4476. {
  4477. struct external_nlist *incl_sym;
  4478. int nest;
  4479. struct aout_link_includes_entry *incl_entry;
  4480. struct aout_link_includes_totals *t;
  4481. val = 0;
  4482. nest = 0;
  4483. for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
  4484. {
  4485. int incl_type;
  4486. incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
  4487. if (incl_type == (int) N_EINCL)
  4488. {
  4489. if (nest == 0)
  4490. break;
  4491. --nest;
  4492. }
  4493. else if (incl_type == (int) N_BINCL)
  4494. ++nest;
  4495. else if (nest == 0)
  4496. {
  4497. const char *s;
  4498. s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
  4499. for (; *s != '\0'; s++)
  4500. {
  4501. val += *s;
  4502. if (*s == '(')
  4503. {
  4504. /* Skip the file number. */
  4505. ++s;
  4506. while (ISDIGIT (*s))
  4507. ++s;
  4508. --s;
  4509. }
  4510. }
  4511. }
  4512. }
  4513. /* If we have already included a header file with the
  4514. same value, then replace this one with an N_EXCL
  4515. symbol. */
  4516. copy = (bfd_boolean) (! flaginfo->info->keep_memory);
  4517. incl_entry = aout_link_includes_lookup (&flaginfo->includes,
  4518. name, TRUE, copy);
  4519. if (incl_entry == NULL)
  4520. return FALSE;
  4521. for (t = incl_entry->totals; t != NULL; t = t->next)
  4522. if (t->total == val)
  4523. break;
  4524. if (t == NULL)
  4525. {
  4526. /* This is the first time we have seen this header
  4527. file with this set of stabs strings. */
  4528. t = (struct aout_link_includes_totals *)
  4529. bfd_hash_allocate (&flaginfo->includes.root,
  4530. sizeof *t);
  4531. if (t == NULL)
  4532. return FALSE;
  4533. t->total = val;
  4534. t->next = incl_entry->totals;
  4535. incl_entry->totals = t;
  4536. }
  4537. else
  4538. {
  4539. int *incl_map;
  4540. /* This is a duplicate header file. We must change
  4541. it to be an N_EXCL entry, and mark all the
  4542. included symbols to prevent outputting them. */
  4543. type = (int) N_EXCL;
  4544. nest = 0;
  4545. for (incl_sym = sym + 1, incl_map = symbol_map + 1;
  4546. incl_sym < sym_end;
  4547. incl_sym++, incl_map++)
  4548. {
  4549. int incl_type;
  4550. incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
  4551. if (incl_type == (int) N_EINCL)
  4552. {
  4553. if (nest == 0)
  4554. {
  4555. *incl_map = -1;
  4556. break;
  4557. }
  4558. --nest;
  4559. }
  4560. else if (incl_type == (int) N_BINCL)
  4561. ++nest;
  4562. else if (nest == 0)
  4563. *incl_map = -1;
  4564. }
  4565. }
  4566. }
  4567. }
  4568. /* Copy this symbol into the list of symbols we are going to
  4569. write out. */
  4570. H_PUT_8 (output_bfd, type, outsym->e_type);
  4571. H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_other), outsym->e_other);
  4572. H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
  4573. copy = FALSE;
  4574. if (! flaginfo->info->keep_memory)
  4575. {
  4576. /* name points into a string table which we are going to
  4577. free. If there is a hash table entry, use that string.
  4578. Otherwise, copy name into memory. */
  4579. if (h != NULL)
  4580. name = h->root.root.string;
  4581. else
  4582. copy = TRUE;
  4583. }
  4584. strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
  4585. name, copy);
  4586. if (strtab_index == (bfd_size_type) -1)
  4587. return FALSE;
  4588. PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
  4589. PUT_WORD (output_bfd, val, outsym->e_value);
  4590. *symbol_map = obj_aout_external_sym_count (output_bfd);
  4591. ++obj_aout_external_sym_count (output_bfd);
  4592. ++outsym;
  4593. }
  4594. /* Write out the output symbols we have just constructed. */
  4595. if (outsym > flaginfo->output_syms)
  4596. {
  4597. bfd_size_type outsym_size;
  4598. if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0)
  4599. return FALSE;
  4600. outsym_size = outsym - flaginfo->output_syms;
  4601. outsym_size *= EXTERNAL_NLIST_SIZE;
  4602. if (bfd_bwrite ((void *) flaginfo->output_syms, outsym_size, output_bfd)
  4603. != outsym_size)
  4604. return FALSE;
  4605. flaginfo->symoff += outsym_size;
  4606. }
  4607. return TRUE;
  4608. }
  4609. /* Link an a.out input BFD into the output file. */
  4610. static bfd_boolean
  4611. aout_link_input_bfd (struct aout_final_link_info *flaginfo, bfd *input_bfd)
  4612. {
  4613. BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
  4614. /* If this is a dynamic object, it may need special handling. */
  4615. if ((input_bfd->flags & DYNAMIC) != 0
  4616. && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
  4617. return ((*aout_backend_info (input_bfd)->link_dynamic_object)
  4618. (flaginfo->info, input_bfd));
  4619. /* Get the symbols. We probably have them already, unless
  4620. flaginfo->info->keep_memory is FALSE. */
  4621. if (! aout_get_external_symbols (input_bfd))
  4622. return FALSE;
  4623. /* Write out the symbols and get a map of the new indices. The map
  4624. is placed into flaginfo->symbol_map. */
  4625. if (! aout_link_write_symbols (flaginfo, input_bfd))
  4626. return FALSE;
  4627. /* Relocate and write out the sections. These functions use the
  4628. symbol map created by aout_link_write_symbols. The linker_mark
  4629. field will be set if these sections are to be included in the
  4630. link, which will normally be the case. */
  4631. if (obj_textsec (input_bfd)->linker_mark)
  4632. {
  4633. if (! aout_link_input_section (flaginfo, input_bfd,
  4634. obj_textsec (input_bfd),
  4635. &flaginfo->treloff,
  4636. exec_hdr (input_bfd)->a_trsize))
  4637. return FALSE;
  4638. }
  4639. if (obj_datasec (input_bfd)->linker_mark)
  4640. {
  4641. if (! aout_link_input_section (flaginfo, input_bfd,
  4642. obj_datasec (input_bfd),
  4643. &flaginfo->dreloff,
  4644. exec_hdr (input_bfd)->a_drsize))
  4645. return FALSE;
  4646. }
  4647. /* If we are not keeping memory, we don't need the symbols any
  4648. longer. We still need them if we are keeping memory, because the
  4649. strings in the hash table point into them. */
  4650. if (! flaginfo->info->keep_memory)
  4651. {
  4652. if (! aout_link_free_symbols (input_bfd))
  4653. return FALSE;
  4654. }
  4655. return TRUE;
  4656. }
  4657. /* Do the final link step. This is called on the output BFD. The
  4658. INFO structure should point to a list of BFDs linked through the
  4659. link.next field which can be used to find each BFD which takes part
  4660. in the output. Also, each section in ABFD should point to a list
  4661. of bfd_link_order structures which list all the input sections for
  4662. the output section. */
  4663. bfd_boolean
  4664. NAME (aout, final_link) (bfd *abfd,
  4665. struct bfd_link_info *info,
  4666. void (*callback) (bfd *, file_ptr *, file_ptr *, file_ptr *))
  4667. {
  4668. struct aout_final_link_info aout_info;
  4669. bfd_boolean includes_hash_initialized = FALSE;
  4670. bfd *sub;
  4671. bfd_size_type trsize, drsize;
  4672. bfd_size_type max_contents_size;
  4673. bfd_size_type max_relocs_size;
  4674. bfd_size_type max_sym_count;
  4675. bfd_size_type text_size;
  4676. file_ptr text_end;
  4677. struct bfd_link_order *p;
  4678. asection *o;
  4679. bfd_boolean have_link_order_relocs;
  4680. if (bfd_link_pic (info))
  4681. abfd->flags |= DYNAMIC;
  4682. aout_info.info = info;
  4683. aout_info.output_bfd = abfd;
  4684. aout_info.contents = NULL;
  4685. aout_info.relocs = NULL;
  4686. aout_info.symbol_map = NULL;
  4687. aout_info.output_syms = NULL;
  4688. if (!bfd_hash_table_init_n (&aout_info.includes.root,
  4689. aout_link_includes_newfunc,
  4690. sizeof (struct aout_link_includes_entry),
  4691. 251))
  4692. goto error_return;
  4693. includes_hash_initialized = TRUE;
  4694. /* Figure out the largest section size. Also, if generating
  4695. relocatable output, count the relocs. */
  4696. trsize = 0;
  4697. drsize = 0;
  4698. max_contents_size = 0;
  4699. max_relocs_size = 0;
  4700. max_sym_count = 0;
  4701. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  4702. {
  4703. bfd_size_type sz;
  4704. if (bfd_link_relocatable (info))
  4705. {
  4706. if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
  4707. {
  4708. trsize += exec_hdr (sub)->a_trsize;
  4709. drsize += exec_hdr (sub)->a_drsize;
  4710. }
  4711. else
  4712. {
  4713. /* FIXME: We need to identify the .text and .data sections
  4714. and call get_reloc_upper_bound and canonicalize_reloc to
  4715. work out the number of relocs needed, and then multiply
  4716. by the reloc size. */
  4717. (*_bfd_error_handler)
  4718. (_("%s: relocatable link from %s to %s not supported"),
  4719. bfd_get_filename (abfd),
  4720. sub->xvec->name, abfd->xvec->name);
  4721. bfd_set_error (bfd_error_invalid_operation);
  4722. goto error_return;
  4723. }
  4724. }
  4725. if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
  4726. {
  4727. sz = obj_textsec (sub)->size;
  4728. if (sz > max_contents_size)
  4729. max_contents_size = sz;
  4730. sz = obj_datasec (sub)->size;
  4731. if (sz > max_contents_size)
  4732. max_contents_size = sz;
  4733. sz = exec_hdr (sub)->a_trsize;
  4734. if (sz > max_relocs_size)
  4735. max_relocs_size = sz;
  4736. sz = exec_hdr (sub)->a_drsize;
  4737. if (sz > max_relocs_size)
  4738. max_relocs_size = sz;
  4739. sz = obj_aout_external_sym_count (sub);
  4740. if (sz > max_sym_count)
  4741. max_sym_count = sz;
  4742. }
  4743. }
  4744. if (bfd_link_relocatable (info))
  4745. {
  4746. if (obj_textsec (abfd) != NULL)
  4747. trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
  4748. ->map_head.link_order)
  4749. * obj_reloc_entry_size (abfd));
  4750. if (obj_datasec (abfd) != NULL)
  4751. drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
  4752. ->map_head.link_order)
  4753. * obj_reloc_entry_size (abfd));
  4754. }
  4755. exec_hdr (abfd)->a_trsize = trsize;
  4756. exec_hdr (abfd)->a_drsize = drsize;
  4757. exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
  4758. /* Adjust the section sizes and vmas according to the magic number.
  4759. This sets a_text, a_data and a_bss in the exec_hdr and sets the
  4760. filepos for each section. */
  4761. if (! NAME (aout, adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
  4762. goto error_return;
  4763. /* The relocation and symbol file positions differ among a.out
  4764. targets. We are passed a callback routine from the backend
  4765. specific code to handle this.
  4766. FIXME: At this point we do not know how much space the symbol
  4767. table will require. This will not work for any (nonstandard)
  4768. a.out target that needs to know the symbol table size before it
  4769. can compute the relocation file positions. This may or may not
  4770. be the case for the hp300hpux target, for example. */
  4771. (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
  4772. &aout_info.symoff);
  4773. obj_textsec (abfd)->rel_filepos = aout_info.treloff;
  4774. obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
  4775. obj_sym_filepos (abfd) = aout_info.symoff;
  4776. /* We keep a count of the symbols as we output them. */
  4777. obj_aout_external_sym_count (abfd) = 0;
  4778. /* We accumulate the string table as we write out the symbols. */
  4779. aout_info.strtab = _bfd_stringtab_init ();
  4780. if (aout_info.strtab == NULL)
  4781. goto error_return;
  4782. /* Allocate buffers to hold section contents and relocs. */
  4783. aout_info.contents = (bfd_byte *) bfd_malloc (max_contents_size);
  4784. aout_info.relocs = bfd_malloc (max_relocs_size);
  4785. aout_info.symbol_map = (int *) bfd_malloc (max_sym_count * sizeof (int));
  4786. aout_info.output_syms = (struct external_nlist *)
  4787. bfd_malloc ((max_sym_count + 1) * sizeof (struct external_nlist));
  4788. if ((aout_info.contents == NULL && max_contents_size != 0)
  4789. || (aout_info.relocs == NULL && max_relocs_size != 0)
  4790. || (aout_info.symbol_map == NULL && max_sym_count != 0)
  4791. || aout_info.output_syms == NULL)
  4792. goto error_return;
  4793. /* If we have a symbol named __DYNAMIC, force it out now. This is
  4794. required by SunOS. Doing this here rather than in sunos.c is a
  4795. hack, but it's easier than exporting everything which would be
  4796. needed. */
  4797. {
  4798. struct aout_link_hash_entry *h;
  4799. h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
  4800. FALSE, FALSE, FALSE);
  4801. if (h != NULL)
  4802. aout_link_write_other_symbol (&h->root.root, &aout_info);
  4803. }
  4804. /* The most time efficient way to do the link would be to read all
  4805. the input object files into memory and then sort out the
  4806. information into the output file. Unfortunately, that will
  4807. probably use too much memory. Another method would be to step
  4808. through everything that composes the text section and write it
  4809. out, and then everything that composes the data section and write
  4810. it out, and then write out the relocs, and then write out the
  4811. symbols. Unfortunately, that requires reading stuff from each
  4812. input file several times, and we will not be able to keep all the
  4813. input files open simultaneously, and reopening them will be slow.
  4814. What we do is basically process one input file at a time. We do
  4815. everything we need to do with an input file once--copy over the
  4816. section contents, handle the relocation information, and write
  4817. out the symbols--and then we throw away the information we read
  4818. from it. This approach requires a lot of lseeks of the output
  4819. file, which is unfortunate but still faster than reopening a lot
  4820. of files.
  4821. We use the output_has_begun field of the input BFDs to see
  4822. whether we have already handled it. */
  4823. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  4824. sub->output_has_begun = FALSE;
  4825. /* Mark all sections which are to be included in the link. This
  4826. will normally be every section. We need to do this so that we
  4827. can identify any sections which the linker has decided to not
  4828. include. */
  4829. for (o = abfd->sections; o != NULL; o = o->next)
  4830. {
  4831. for (p = o->map_head.link_order; p != NULL; p = p->next)
  4832. if (p->type == bfd_indirect_link_order)
  4833. p->u.indirect.section->linker_mark = TRUE;
  4834. }
  4835. have_link_order_relocs = FALSE;
  4836. for (o = abfd->sections; o != NULL; o = o->next)
  4837. {
  4838. for (p = o->map_head.link_order;
  4839. p != NULL;
  4840. p = p->next)
  4841. {
  4842. if (p->type == bfd_indirect_link_order
  4843. && (bfd_get_flavour (p->u.indirect.section->owner)
  4844. == bfd_target_aout_flavour))
  4845. {
  4846. bfd *input_bfd;
  4847. input_bfd = p->u.indirect.section->owner;
  4848. if (! input_bfd->output_has_begun)
  4849. {
  4850. if (! aout_link_input_bfd (&aout_info, input_bfd))
  4851. goto error_return;
  4852. input_bfd->output_has_begun = TRUE;
  4853. }
  4854. }
  4855. else if (p->type == bfd_section_reloc_link_order
  4856. || p->type == bfd_symbol_reloc_link_order)
  4857. {
  4858. /* These are handled below. */
  4859. have_link_order_relocs = TRUE;
  4860. }
  4861. else
  4862. {
  4863. if (! _bfd_default_link_order (abfd, info, o, p))
  4864. goto error_return;
  4865. }
  4866. }
  4867. }
  4868. /* Write out any symbols that we have not already written out. */
  4869. bfd_hash_traverse (&info->hash->table,
  4870. aout_link_write_other_symbol,
  4871. &aout_info);
  4872. /* Now handle any relocs we were asked to create by the linker.
  4873. These did not come from any input file. We must do these after
  4874. we have written out all the symbols, so that we know the symbol
  4875. indices to use. */
  4876. if (have_link_order_relocs)
  4877. {
  4878. for (o = abfd->sections; o != NULL; o = o->next)
  4879. {
  4880. for (p = o->map_head.link_order;
  4881. p != NULL;
  4882. p = p->next)
  4883. {
  4884. if (p->type == bfd_section_reloc_link_order
  4885. || p->type == bfd_symbol_reloc_link_order)
  4886. {
  4887. if (! aout_link_reloc_link_order (&aout_info, o, p))
  4888. goto error_return;
  4889. }
  4890. }
  4891. }
  4892. }
  4893. if (aout_info.contents != NULL)
  4894. {
  4895. free (aout_info.contents);
  4896. aout_info.contents = NULL;
  4897. }
  4898. if (aout_info.relocs != NULL)
  4899. {
  4900. free (aout_info.relocs);
  4901. aout_info.relocs = NULL;
  4902. }
  4903. if (aout_info.symbol_map != NULL)
  4904. {
  4905. free (aout_info.symbol_map);
  4906. aout_info.symbol_map = NULL;
  4907. }
  4908. if (aout_info.output_syms != NULL)
  4909. {
  4910. free (aout_info.output_syms);
  4911. aout_info.output_syms = NULL;
  4912. }
  4913. if (includes_hash_initialized)
  4914. {
  4915. bfd_hash_table_free (&aout_info.includes.root);
  4916. includes_hash_initialized = FALSE;
  4917. }
  4918. /* Finish up any dynamic linking we may be doing. */
  4919. if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
  4920. {
  4921. if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
  4922. goto error_return;
  4923. }
  4924. /* Update the header information. */
  4925. abfd->symcount = obj_aout_external_sym_count (abfd);
  4926. exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
  4927. obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
  4928. obj_textsec (abfd)->reloc_count =
  4929. exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
  4930. obj_datasec (abfd)->reloc_count =
  4931. exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
  4932. /* Write out the string table, unless there are no symbols. */
  4933. if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
  4934. goto error_return;
  4935. if (abfd->symcount > 0)
  4936. {
  4937. if (!emit_stringtab (abfd, aout_info.strtab))
  4938. goto error_return;
  4939. }
  4940. else
  4941. {
  4942. bfd_byte b[BYTES_IN_WORD];
  4943. memset (b, 0, BYTES_IN_WORD);
  4944. if (bfd_bwrite (b, (bfd_size_type) BYTES_IN_WORD, abfd) != BYTES_IN_WORD)
  4945. goto error_return;
  4946. }
  4947. return TRUE;
  4948. error_return:
  4949. if (aout_info.contents != NULL)
  4950. free (aout_info.contents);
  4951. if (aout_info.relocs != NULL)
  4952. free (aout_info.relocs);
  4953. if (aout_info.symbol_map != NULL)
  4954. free (aout_info.symbol_map);
  4955. if (aout_info.output_syms != NULL)
  4956. free (aout_info.output_syms);
  4957. if (includes_hash_initialized)
  4958. bfd_hash_table_free (&aout_info.includes.root);
  4959. return FALSE;
  4960. }