aoutx.h 161 KB

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