ecoff.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482
  1. /* Generic ECOFF (Extended-COFF) routines.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Original version by Per Bothner.
  4. Full support added by Ian Lance Taylor, ian@cygnus.com.
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "bfdlink.h"
  21. #include "libbfd.h"
  22. #include "aout/ar.h"
  23. #include "aout/stab_gnu.h"
  24. /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
  25. some other stuff which we don't want and which conflicts with stuff
  26. we do want. */
  27. #include "libaout.h"
  28. #include "aout/aout64.h"
  29. #undef N_ABS
  30. #undef exec_hdr
  31. #undef obj_sym_filepos
  32. #include "coff/internal.h"
  33. #include "coff/sym.h"
  34. #include "coff/symconst.h"
  35. #include "coff/ecoff.h"
  36. #include "libcoff.h"
  37. #include "libecoff.h"
  38. #include "libiberty.h"
  39. #define streq(a, b) (strcmp ((a), (b)) == 0)
  40. #define strneq(a, b, n) (strncmp ((a), (b), (n)) == 0)
  41. /* This stuff is somewhat copied from coffcode.h. */
  42. static asection bfd_debug_section =
  43. {
  44. /* name, id, index, next, prev, flags, user_set_vma, */
  45. "*DEBUG*", 0, 0, NULL, NULL, 0, 0,
  46. /* linker_mark, linker_has_input, gc_mark, compress_status, */
  47. 0, 0, 1, 0,
  48. /* segment_mark, sec_info_type, use_rela_p, */
  49. 0, 0, 0,
  50. /* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5, */
  51. 0, 0, 0, 0, 0, 0,
  52. /* vma, lma, size, rawsize, compressed_size, relax, relax_count, */
  53. 0, 0, 0, 0, 0, 0, 0,
  54. /* output_offset, output_section, alignment_power, */
  55. 0, NULL, 0,
  56. /* relocation, orelocation, reloc_count, filepos, rel_filepos, */
  57. NULL, NULL, 0, 0, 0,
  58. /* line_filepos, userdata, contents, lineno, lineno_count, */
  59. 0, NULL, NULL, NULL, 0,
  60. /* entsize, kept_section, moving_line_filepos, */
  61. 0, NULL, 0,
  62. /* target_index, used_by_bfd, constructor_chain, owner, */
  63. 0, NULL, NULL, NULL,
  64. /* symbol, */
  65. NULL,
  66. /* symbol_ptr_ptr, */
  67. NULL,
  68. /* map_head, map_tail */
  69. { NULL }, { NULL }
  70. };
  71. /* Create an ECOFF object. */
  72. bfd_boolean
  73. _bfd_ecoff_mkobject (bfd *abfd)
  74. {
  75. bfd_size_type amt = sizeof (ecoff_data_type);
  76. abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
  77. if (abfd->tdata.ecoff_obj_data == NULL)
  78. return FALSE;
  79. return TRUE;
  80. }
  81. /* This is a hook called by coff_real_object_p to create any backend
  82. specific information. */
  83. void *
  84. _bfd_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr)
  85. {
  86. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  87. struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
  88. ecoff_data_type *ecoff;
  89. if (! _bfd_ecoff_mkobject (abfd))
  90. return NULL;
  91. ecoff = ecoff_data (abfd);
  92. ecoff->gp_size = 8;
  93. ecoff->sym_filepos = internal_f->f_symptr;
  94. if (internal_a != NULL)
  95. {
  96. int i;
  97. ecoff->text_start = internal_a->text_start;
  98. ecoff->text_end = internal_a->text_start + internal_a->tsize;
  99. ecoff->gp = internal_a->gp_value;
  100. ecoff->gprmask = internal_a->gprmask;
  101. for (i = 0; i < 4; i++)
  102. ecoff->cprmask[i] = internal_a->cprmask[i];
  103. ecoff->fprmask = internal_a->fprmask;
  104. if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
  105. abfd->flags |= D_PAGED;
  106. else
  107. abfd->flags &=~ D_PAGED;
  108. }
  109. /* It turns out that no special action is required by the MIPS or
  110. Alpha ECOFF backends. They have different information in the
  111. a.out header, but we just copy it all (e.g., gprmask, cprmask and
  112. fprmask) and let the swapping routines ensure that only relevant
  113. information is written out. */
  114. return (void *) ecoff;
  115. }
  116. /* Initialize a new section. */
  117. bfd_boolean
  118. _bfd_ecoff_new_section_hook (bfd *abfd, asection *section)
  119. {
  120. unsigned int i;
  121. static struct
  122. {
  123. const char * name;
  124. flagword flags;
  125. }
  126. section_flags [] =
  127. {
  128. { _TEXT, SEC_ALLOC | SEC_CODE | SEC_LOAD },
  129. { _INIT, SEC_ALLOC | SEC_CODE | SEC_LOAD },
  130. { _FINI, SEC_ALLOC | SEC_CODE | SEC_LOAD },
  131. { _DATA, SEC_ALLOC | SEC_DATA | SEC_LOAD },
  132. { _SDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD },
  133. { _RDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
  134. { _LIT8, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
  135. { _LIT4, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
  136. { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
  137. { _PDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
  138. { _BSS, SEC_ALLOC},
  139. { _SBSS, SEC_ALLOC},
  140. /* An Irix 4 shared libary. */
  141. { _LIB, SEC_COFF_SHARED_LIBRARY}
  142. };
  143. section->alignment_power = 4;
  144. for (i = 0; i < ARRAY_SIZE (section_flags); i++)
  145. if (streq (section->name, section_flags[i].name))
  146. {
  147. section->flags |= section_flags[i].flags;
  148. break;
  149. }
  150. /* Probably any other section name is SEC_NEVER_LOAD, but I'm
  151. uncertain about .init on some systems and I don't know how shared
  152. libraries work. */
  153. return _bfd_generic_new_section_hook (abfd, section);
  154. }
  155. /* Determine the machine architecture and type. This is called from
  156. the generic COFF routines. It is the inverse of ecoff_get_magic,
  157. below. This could be an ECOFF backend routine, with one version
  158. for each target, but there aren't all that many ECOFF targets. */
  159. bfd_boolean
  160. _bfd_ecoff_set_arch_mach_hook (bfd *abfd, void * filehdr)
  161. {
  162. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  163. enum bfd_architecture arch;
  164. unsigned long mach;
  165. switch (internal_f->f_magic)
  166. {
  167. case MIPS_MAGIC_1:
  168. case MIPS_MAGIC_LITTLE:
  169. case MIPS_MAGIC_BIG:
  170. arch = bfd_arch_mips;
  171. mach = bfd_mach_mips3000;
  172. break;
  173. case MIPS_MAGIC_LITTLE2:
  174. case MIPS_MAGIC_BIG2:
  175. /* MIPS ISA level 2: the r6000. */
  176. arch = bfd_arch_mips;
  177. mach = bfd_mach_mips6000;
  178. break;
  179. case MIPS_MAGIC_LITTLE3:
  180. case MIPS_MAGIC_BIG3:
  181. /* MIPS ISA level 3: the r4000. */
  182. arch = bfd_arch_mips;
  183. mach = bfd_mach_mips4000;
  184. break;
  185. case ALPHA_MAGIC:
  186. arch = bfd_arch_alpha;
  187. mach = 0;
  188. break;
  189. default:
  190. arch = bfd_arch_obscure;
  191. mach = 0;
  192. break;
  193. }
  194. return bfd_default_set_arch_mach (abfd, arch, mach);
  195. }
  196. bfd_boolean
  197. _bfd_ecoff_no_long_sections (bfd *abfd, int enable)
  198. {
  199. (void) abfd;
  200. (void) enable;
  201. return FALSE;
  202. }
  203. /* Get the magic number to use based on the architecture and machine.
  204. This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
  205. static int
  206. ecoff_get_magic (bfd *abfd)
  207. {
  208. int big, little;
  209. switch (bfd_get_arch (abfd))
  210. {
  211. case bfd_arch_mips:
  212. switch (bfd_get_mach (abfd))
  213. {
  214. default:
  215. case 0:
  216. case bfd_mach_mips3000:
  217. big = MIPS_MAGIC_BIG;
  218. little = MIPS_MAGIC_LITTLE;
  219. break;
  220. case bfd_mach_mips6000:
  221. big = MIPS_MAGIC_BIG2;
  222. little = MIPS_MAGIC_LITTLE2;
  223. break;
  224. case bfd_mach_mips4000:
  225. big = MIPS_MAGIC_BIG3;
  226. little = MIPS_MAGIC_LITTLE3;
  227. break;
  228. }
  229. return bfd_big_endian (abfd) ? big : little;
  230. case bfd_arch_alpha:
  231. return ALPHA_MAGIC;
  232. default:
  233. abort ();
  234. return 0;
  235. }
  236. }
  237. /* Get the section s_flags to use for a section. */
  238. static long
  239. ecoff_sec_to_styp_flags (const char *name, flagword flags)
  240. {
  241. unsigned int i;
  242. static struct
  243. {
  244. const char * name;
  245. long flags;
  246. }
  247. styp_flags [] =
  248. {
  249. { _TEXT, STYP_TEXT },
  250. { _DATA, STYP_DATA },
  251. { _SDATA, STYP_SDATA },
  252. { _RDATA, STYP_RDATA },
  253. { _LITA, STYP_LITA },
  254. { _LIT8, STYP_LIT8 },
  255. { _LIT4, STYP_LIT4 },
  256. { _BSS, STYP_BSS },
  257. { _SBSS, STYP_SBSS },
  258. { _INIT, STYP_ECOFF_INIT },
  259. { _FINI, STYP_ECOFF_FINI },
  260. { _PDATA, STYP_PDATA },
  261. { _XDATA, STYP_XDATA },
  262. { _LIB, STYP_ECOFF_LIB },
  263. { _GOT, STYP_GOT },
  264. { _HASH, STYP_HASH },
  265. { _DYNAMIC, STYP_DYNAMIC },
  266. { _LIBLIST, STYP_LIBLIST },
  267. { _RELDYN, STYP_RELDYN },
  268. { _CONFLIC, STYP_CONFLIC },
  269. { _DYNSTR, STYP_DYNSTR },
  270. { _DYNSYM, STYP_DYNSYM },
  271. { _RCONST, STYP_RCONST }
  272. };
  273. long styp = 0;
  274. for (i = 0; i < ARRAY_SIZE (styp_flags); i++)
  275. if (streq (name, styp_flags[i].name))
  276. {
  277. styp = styp_flags[i].flags;
  278. break;
  279. }
  280. if (styp == 0)
  281. {
  282. if (streq (name, _COMMENT))
  283. {
  284. styp = STYP_COMMENT;
  285. flags &=~ SEC_NEVER_LOAD;
  286. }
  287. else if (flags & SEC_CODE)
  288. styp = STYP_TEXT;
  289. else if (flags & SEC_DATA)
  290. styp = STYP_DATA;
  291. else if (flags & SEC_READONLY)
  292. styp = STYP_RDATA;
  293. else if (flags & SEC_LOAD)
  294. styp = STYP_REG;
  295. else
  296. styp = STYP_BSS;
  297. }
  298. if (flags & SEC_NEVER_LOAD)
  299. styp |= STYP_NOLOAD;
  300. return styp;
  301. }
  302. /* Get the BFD flags to use for a section. */
  303. bfd_boolean
  304. _bfd_ecoff_styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
  305. void * hdr,
  306. const char *name ATTRIBUTE_UNUSED,
  307. asection *section ATTRIBUTE_UNUSED,
  308. flagword * flags_ptr)
  309. {
  310. struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  311. long styp_flags = internal_s->s_flags;
  312. flagword sec_flags = 0;
  313. if (styp_flags & STYP_NOLOAD)
  314. sec_flags |= SEC_NEVER_LOAD;
  315. /* For 386 COFF, at least, an unloadable text or data section is
  316. actually a shared library section. */
  317. if ((styp_flags & STYP_TEXT)
  318. || (styp_flags & STYP_ECOFF_INIT)
  319. || (styp_flags & STYP_ECOFF_FINI)
  320. || (styp_flags & STYP_DYNAMIC)
  321. || (styp_flags & STYP_LIBLIST)
  322. || (styp_flags & STYP_RELDYN)
  323. || styp_flags == STYP_CONFLIC
  324. || (styp_flags & STYP_DYNSTR)
  325. || (styp_flags & STYP_DYNSYM)
  326. || (styp_flags & STYP_HASH))
  327. {
  328. if (sec_flags & SEC_NEVER_LOAD)
  329. sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
  330. else
  331. sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  332. }
  333. else if ((styp_flags & STYP_DATA)
  334. || (styp_flags & STYP_RDATA)
  335. || (styp_flags & STYP_SDATA)
  336. || styp_flags == STYP_PDATA
  337. || styp_flags == STYP_XDATA
  338. || (styp_flags & STYP_GOT)
  339. || styp_flags == STYP_RCONST)
  340. {
  341. if (sec_flags & SEC_NEVER_LOAD)
  342. sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
  343. else
  344. sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  345. if ((styp_flags & STYP_RDATA)
  346. || styp_flags == STYP_PDATA
  347. || styp_flags == STYP_RCONST)
  348. sec_flags |= SEC_READONLY;
  349. }
  350. else if ((styp_flags & STYP_BSS)
  351. || (styp_flags & STYP_SBSS))
  352. sec_flags |= SEC_ALLOC;
  353. else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
  354. sec_flags |= SEC_NEVER_LOAD;
  355. else if ((styp_flags & STYP_LITA)
  356. || (styp_flags & STYP_LIT8)
  357. || (styp_flags & STYP_LIT4))
  358. sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
  359. else if (styp_flags & STYP_ECOFF_LIB)
  360. sec_flags |= SEC_COFF_SHARED_LIBRARY;
  361. else
  362. sec_flags |= SEC_ALLOC | SEC_LOAD;
  363. * flags_ptr = sec_flags;
  364. return TRUE;
  365. }
  366. /* Read in the symbolic header for an ECOFF object file. */
  367. static bfd_boolean
  368. ecoff_slurp_symbolic_header (bfd *abfd)
  369. {
  370. const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
  371. bfd_size_type external_hdr_size;
  372. void * raw = NULL;
  373. HDRR *internal_symhdr;
  374. /* See if we've already read it in. */
  375. if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
  376. backend->debug_swap.sym_magic)
  377. return TRUE;
  378. /* See whether there is a symbolic header. */
  379. if (ecoff_data (abfd)->sym_filepos == 0)
  380. {
  381. bfd_get_symcount (abfd) = 0;
  382. return TRUE;
  383. }
  384. /* At this point bfd_get_symcount (abfd) holds the number of symbols
  385. as read from the file header, but on ECOFF this is always the
  386. size of the symbolic information header. It would be cleaner to
  387. handle this when we first read the file in coffgen.c. */
  388. external_hdr_size = backend->debug_swap.external_hdr_size;
  389. if (bfd_get_symcount (abfd) != external_hdr_size)
  390. {
  391. bfd_set_error (bfd_error_bad_value);
  392. return FALSE;
  393. }
  394. /* Read the symbolic information header. */
  395. raw = bfd_malloc (external_hdr_size);
  396. if (raw == NULL)
  397. goto error_return;
  398. if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0
  399. || bfd_bread (raw, external_hdr_size, abfd) != external_hdr_size)
  400. goto error_return;
  401. internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
  402. (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
  403. if (internal_symhdr->magic != backend->debug_swap.sym_magic)
  404. {
  405. bfd_set_error (bfd_error_bad_value);
  406. goto error_return;
  407. }
  408. /* Now we can get the correct number of symbols. */
  409. bfd_get_symcount (abfd) = (internal_symhdr->isymMax
  410. + internal_symhdr->iextMax);
  411. if (raw != NULL)
  412. free (raw);
  413. return TRUE;
  414. error_return:
  415. if (raw != NULL)
  416. free (raw);
  417. return FALSE;
  418. }
  419. /* Read in and swap the important symbolic information for an ECOFF
  420. object file. This is called by gdb via the read_debug_info entry
  421. point in the backend structure. */
  422. bfd_boolean
  423. _bfd_ecoff_slurp_symbolic_info (bfd *abfd,
  424. asection *ignore ATTRIBUTE_UNUSED,
  425. struct ecoff_debug_info *debug)
  426. {
  427. const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
  428. HDRR *internal_symhdr;
  429. bfd_size_type raw_base;
  430. bfd_size_type raw_size;
  431. void * raw;
  432. bfd_size_type external_fdr_size;
  433. char *fraw_src;
  434. char *fraw_end;
  435. struct fdr *fdr_ptr;
  436. bfd_size_type raw_end;
  437. bfd_size_type cb_end;
  438. file_ptr pos;
  439. BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
  440. /* Check whether we've already gotten it, and whether there's any to
  441. get. */
  442. if (ecoff_data (abfd)->raw_syments != NULL)
  443. return TRUE;
  444. if (ecoff_data (abfd)->sym_filepos == 0)
  445. {
  446. bfd_get_symcount (abfd) = 0;
  447. return TRUE;
  448. }
  449. if (! ecoff_slurp_symbolic_header (abfd))
  450. return FALSE;
  451. internal_symhdr = &debug->symbolic_header;
  452. /* Read all the symbolic information at once. */
  453. raw_base = (ecoff_data (abfd)->sym_filepos
  454. + backend->debug_swap.external_hdr_size);
  455. /* Alpha ecoff makes the determination of raw_size difficult. It has
  456. an undocumented debug data section between the symhdr and the first
  457. documented section. And the ordering of the sections varies between
  458. statically and dynamically linked executables.
  459. If bfd supports SEEK_END someday, this code could be simplified. */
  460. raw_end = 0;
  461. #define UPDATE_RAW_END(start, count, size) \
  462. cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
  463. if (cb_end > raw_end) \
  464. raw_end = cb_end
  465. UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
  466. UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
  467. UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
  468. UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
  469. /* eraxxon@alumni.rice.edu: ioptMax refers to the size of the
  470. optimization symtab, not the number of entries. */
  471. UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
  472. UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
  473. UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
  474. UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
  475. UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
  476. UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
  477. UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
  478. #undef UPDATE_RAW_END
  479. raw_size = raw_end - raw_base;
  480. if (raw_size == 0)
  481. {
  482. ecoff_data (abfd)->sym_filepos = 0;
  483. return TRUE;
  484. }
  485. raw = bfd_alloc (abfd, raw_size);
  486. if (raw == NULL)
  487. return FALSE;
  488. pos = ecoff_data (abfd)->sym_filepos;
  489. pos += backend->debug_swap.external_hdr_size;
  490. if (bfd_seek (abfd, pos, SEEK_SET) != 0
  491. || bfd_bread (raw, raw_size, abfd) != raw_size)
  492. {
  493. bfd_release (abfd, raw);
  494. return FALSE;
  495. }
  496. ecoff_data (abfd)->raw_syments = raw;
  497. /* Get pointers for the numeric offsets in the HDRR structure. */
  498. #define FIX(off1, off2, type) \
  499. if (internal_symhdr->off1 == 0) \
  500. debug->off2 = NULL; \
  501. else \
  502. debug->off2 = (type) ((char *) raw \
  503. + (internal_symhdr->off1 \
  504. - raw_base))
  505. FIX (cbLineOffset, line, unsigned char *);
  506. FIX (cbDnOffset, external_dnr, void *);
  507. FIX (cbPdOffset, external_pdr, void *);
  508. FIX (cbSymOffset, external_sym, void *);
  509. FIX (cbOptOffset, external_opt, void *);
  510. FIX (cbAuxOffset, external_aux, union aux_ext *);
  511. FIX (cbSsOffset, ss, char *);
  512. FIX (cbSsExtOffset, ssext, char *);
  513. FIX (cbFdOffset, external_fdr, void *);
  514. FIX (cbRfdOffset, external_rfd, void *);
  515. FIX (cbExtOffset, external_ext, void *);
  516. #undef FIX
  517. /* I don't want to always swap all the data, because it will just
  518. waste time and most programs will never look at it. The only
  519. time the linker needs most of the debugging information swapped
  520. is when linking big-endian and little-endian MIPS object files
  521. together, which is not a common occurrence.
  522. We need to look at the fdr to deal with a lot of information in
  523. the symbols, so we swap them here. */
  524. debug->fdr = (FDR *) bfd_alloc2 (abfd, internal_symhdr->ifdMax,
  525. sizeof (struct fdr));
  526. if (debug->fdr == NULL)
  527. return FALSE;
  528. external_fdr_size = backend->debug_swap.external_fdr_size;
  529. fdr_ptr = debug->fdr;
  530. fraw_src = (char *) debug->external_fdr;
  531. /* PR 17512: file: 3372-1243-0.004. */
  532. if (fraw_src == NULL && internal_symhdr->ifdMax > 0)
  533. return FALSE;
  534. fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
  535. for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
  536. (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr);
  537. return TRUE;
  538. }
  539. /* ECOFF symbol table routines. The ECOFF symbol table is described
  540. in gcc/mips-tfile.c. */
  541. /* ECOFF uses two common sections. One is the usual one, and the
  542. other is for small objects. All the small objects are kept
  543. together, and then referenced via the gp pointer, which yields
  544. faster assembler code. This is what we use for the small common
  545. section. */
  546. static asection ecoff_scom_section;
  547. static asymbol ecoff_scom_symbol;
  548. static asymbol *ecoff_scom_symbol_ptr;
  549. /* Create an empty symbol. */
  550. asymbol *
  551. _bfd_ecoff_make_empty_symbol (bfd *abfd)
  552. {
  553. ecoff_symbol_type *new_symbol;
  554. bfd_size_type amt = sizeof (ecoff_symbol_type);
  555. new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
  556. if (new_symbol == NULL)
  557. return NULL;
  558. new_symbol->symbol.section = NULL;
  559. new_symbol->fdr = NULL;
  560. new_symbol->local = FALSE;
  561. new_symbol->native = NULL;
  562. new_symbol->symbol.the_bfd = abfd;
  563. return &new_symbol->symbol;
  564. }
  565. /* Set the BFD flags and section for an ECOFF symbol. */
  566. static bfd_boolean
  567. ecoff_set_symbol_info (bfd *abfd,
  568. SYMR *ecoff_sym,
  569. asymbol *asym,
  570. int ext,
  571. int weak)
  572. {
  573. asym->the_bfd = abfd;
  574. asym->value = ecoff_sym->value;
  575. asym->section = &bfd_debug_section;
  576. asym->udata.i = 0;
  577. /* Most symbol types are just for debugging. */
  578. switch (ecoff_sym->st)
  579. {
  580. case stGlobal:
  581. case stStatic:
  582. case stLabel:
  583. case stProc:
  584. case stStaticProc:
  585. break;
  586. case stNil:
  587. if (ECOFF_IS_STAB (ecoff_sym))
  588. {
  589. asym->flags = BSF_DEBUGGING;
  590. return TRUE;
  591. }
  592. break;
  593. default:
  594. asym->flags = BSF_DEBUGGING;
  595. return TRUE;
  596. }
  597. if (weak)
  598. asym->flags = BSF_EXPORT | BSF_WEAK;
  599. else if (ext)
  600. asym->flags = BSF_EXPORT | BSF_GLOBAL;
  601. else
  602. {
  603. asym->flags = BSF_LOCAL;
  604. /* Normally, a local stProc symbol will have a corresponding
  605. external symbol. We mark the local symbol as a debugging
  606. symbol, in order to prevent nm from printing both out.
  607. Similarly, we mark stLabel and stabs symbols as debugging
  608. symbols. In both cases, we do want to set the value
  609. correctly based on the symbol class. */
  610. if (ecoff_sym->st == stProc
  611. || ecoff_sym->st == stLabel
  612. || ECOFF_IS_STAB (ecoff_sym))
  613. asym->flags |= BSF_DEBUGGING;
  614. }
  615. if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
  616. asym->flags |= BSF_FUNCTION;
  617. switch (ecoff_sym->sc)
  618. {
  619. case scNil:
  620. /* Used for compiler generated labels. Leave them in the
  621. debugging section, and mark them as local. If BSF_DEBUGGING
  622. is set, then nm does not display them for some reason. If no
  623. flags are set then the linker whines about them. */
  624. asym->flags = BSF_LOCAL;
  625. break;
  626. case scText:
  627. asym->section = bfd_make_section_old_way (abfd, _TEXT);
  628. asym->value -= asym->section->vma;
  629. break;
  630. case scData:
  631. asym->section = bfd_make_section_old_way (abfd, _DATA);
  632. asym->value -= asym->section->vma;
  633. break;
  634. case scBss:
  635. asym->section = bfd_make_section_old_way (abfd, _BSS);
  636. asym->value -= asym->section->vma;
  637. break;
  638. case scRegister:
  639. asym->flags = BSF_DEBUGGING;
  640. break;
  641. case scAbs:
  642. asym->section = bfd_abs_section_ptr;
  643. break;
  644. case scUndefined:
  645. asym->section = bfd_und_section_ptr;
  646. asym->flags = 0;
  647. asym->value = 0;
  648. break;
  649. case scCdbLocal:
  650. case scBits:
  651. case scCdbSystem:
  652. case scRegImage:
  653. case scInfo:
  654. case scUserStruct:
  655. asym->flags = BSF_DEBUGGING;
  656. break;
  657. case scSData:
  658. asym->section = bfd_make_section_old_way (abfd, ".sdata");
  659. asym->value -= asym->section->vma;
  660. break;
  661. case scSBss:
  662. asym->section = bfd_make_section_old_way (abfd, ".sbss");
  663. asym->value -= asym->section->vma;
  664. break;
  665. case scRData:
  666. asym->section = bfd_make_section_old_way (abfd, ".rdata");
  667. asym->value -= asym->section->vma;
  668. break;
  669. case scVar:
  670. asym->flags = BSF_DEBUGGING;
  671. break;
  672. case scCommon:
  673. if (asym->value > ecoff_data (abfd)->gp_size)
  674. {
  675. asym->section = bfd_com_section_ptr;
  676. asym->flags = 0;
  677. break;
  678. }
  679. /* Fall through. */
  680. case scSCommon:
  681. if (ecoff_scom_section.name == NULL)
  682. {
  683. /* Initialize the small common section. */
  684. ecoff_scom_section.name = SCOMMON;
  685. ecoff_scom_section.flags = SEC_IS_COMMON;
  686. ecoff_scom_section.output_section = &ecoff_scom_section;
  687. ecoff_scom_section.symbol = &ecoff_scom_symbol;
  688. ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
  689. ecoff_scom_symbol.name = SCOMMON;
  690. ecoff_scom_symbol.flags = BSF_SECTION_SYM;
  691. ecoff_scom_symbol.section = &ecoff_scom_section;
  692. ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
  693. }
  694. asym->section = &ecoff_scom_section;
  695. asym->flags = 0;
  696. break;
  697. case scVarRegister:
  698. case scVariant:
  699. asym->flags = BSF_DEBUGGING;
  700. break;
  701. case scSUndefined:
  702. asym->section = bfd_und_section_ptr;
  703. asym->flags = 0;
  704. asym->value = 0;
  705. break;
  706. case scInit:
  707. asym->section = bfd_make_section_old_way (abfd, ".init");
  708. asym->value -= asym->section->vma;
  709. break;
  710. case scBasedVar:
  711. case scXData:
  712. case scPData:
  713. asym->flags = BSF_DEBUGGING;
  714. break;
  715. case scFini:
  716. asym->section = bfd_make_section_old_way (abfd, ".fini");
  717. asym->value -= asym->section->vma;
  718. break;
  719. case scRConst:
  720. asym->section = bfd_make_section_old_way (abfd, ".rconst");
  721. asym->value -= asym->section->vma;
  722. break;
  723. default:
  724. break;
  725. }
  726. /* Look for special constructors symbols and make relocation entries
  727. in a special construction section. These are produced by the
  728. -fgnu-linker argument to g++. */
  729. if (ECOFF_IS_STAB (ecoff_sym))
  730. {
  731. switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
  732. {
  733. default:
  734. break;
  735. case N_SETA:
  736. case N_SETT:
  737. case N_SETD:
  738. case N_SETB:
  739. /* Mark the symbol as a constructor. */
  740. asym->flags |= BSF_CONSTRUCTOR;
  741. break;
  742. }
  743. }
  744. return TRUE;
  745. }
  746. /* Read an ECOFF symbol table. */
  747. bfd_boolean
  748. _bfd_ecoff_slurp_symbol_table (bfd *abfd)
  749. {
  750. const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
  751. const bfd_size_type external_ext_size
  752. = backend->debug_swap.external_ext_size;
  753. const bfd_size_type external_sym_size
  754. = backend->debug_swap.external_sym_size;
  755. void (* const swap_ext_in) (bfd *, void *, EXTR *)
  756. = backend->debug_swap.swap_ext_in;
  757. void (* const swap_sym_in) (bfd *, void *, SYMR *)
  758. = backend->debug_swap.swap_sym_in;
  759. ecoff_symbol_type *internal;
  760. ecoff_symbol_type *internal_ptr;
  761. char *eraw_src;
  762. char *eraw_end;
  763. FDR *fdr_ptr;
  764. FDR *fdr_end;
  765. /* If we've already read in the symbol table, do nothing. */
  766. if (ecoff_data (abfd)->canonical_symbols != NULL)
  767. return TRUE;
  768. /* Get the symbolic information. */
  769. if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
  770. &ecoff_data (abfd)->debug_info))
  771. return FALSE;
  772. if (bfd_get_symcount (abfd) == 0)
  773. return TRUE;
  774. internal = (ecoff_symbol_type *) bfd_alloc2 (abfd, bfd_get_symcount (abfd),
  775. sizeof (ecoff_symbol_type));
  776. if (internal == NULL)
  777. return FALSE;
  778. internal_ptr = internal;
  779. eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
  780. eraw_end = (eraw_src
  781. + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
  782. * external_ext_size));
  783. for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
  784. {
  785. EXTR internal_esym;
  786. (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym);
  787. /* PR 17512: file: 3372-1000-0.004. */
  788. if (internal_esym.asym.iss >= ecoff_data (abfd)->debug_info.symbolic_header.issExtMax
  789. || internal_esym.asym.iss < 0)
  790. return FALSE;
  791. internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
  792. + internal_esym.asym.iss);
  793. if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
  794. &internal_ptr->symbol, 1,
  795. internal_esym.weakext))
  796. return FALSE;
  797. /* The alpha uses a negative ifd field for section symbols. */
  798. if (internal_esym.ifd >= 0)
  799. {
  800. /* PR 17512: file: 3372-1983-0.004. */
  801. if (internal_esym.ifd >= ecoff_data (abfd)->debug_info.symbolic_header.ifdMax)
  802. internal_ptr->fdr = NULL;
  803. else
  804. internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
  805. + internal_esym.ifd);
  806. }
  807. else
  808. internal_ptr->fdr = NULL;
  809. internal_ptr->local = FALSE;
  810. internal_ptr->native = (void *) eraw_src;
  811. }
  812. /* The local symbols must be accessed via the fdr's, because the
  813. string and aux indices are relative to the fdr information. */
  814. fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
  815. fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
  816. for (; fdr_ptr < fdr_end; fdr_ptr++)
  817. {
  818. char *lraw_src;
  819. char *lraw_end;
  820. lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
  821. + fdr_ptr->isymBase * external_sym_size);
  822. lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
  823. for (;
  824. lraw_src < lraw_end;
  825. lraw_src += external_sym_size, internal_ptr++)
  826. {
  827. SYMR internal_sym;
  828. (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym);
  829. internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
  830. + fdr_ptr->issBase
  831. + internal_sym.iss);
  832. if (!ecoff_set_symbol_info (abfd, &internal_sym,
  833. &internal_ptr->symbol, 0, 0))
  834. return FALSE;
  835. internal_ptr->fdr = fdr_ptr;
  836. internal_ptr->local = TRUE;
  837. internal_ptr->native = (void *) lraw_src;
  838. }
  839. }
  840. /* PR 17512: file: 3372-3080-0.004.
  841. A discrepancy between ecoff_data (abfd)->debug_info.symbolic_header.isymMax
  842. and ecoff_data (abfd)->debug_info.symbolic_header.ifdMax can mean that
  843. we have fewer symbols than we were expecting. Allow for this by updating
  844. the symbol count and warning the user. */
  845. if (internal_ptr - internal < (ptrdiff_t) bfd_get_symcount (abfd))
  846. {
  847. bfd_get_symcount (abfd) = internal_ptr - internal;
  848. (*_bfd_error_handler)
  849. (_("%B: warning: isymMax (%ld) is greater than ifdMax (%d)\n"),
  850. abfd, ecoff_data (abfd)->debug_info.symbolic_header.isymMax,
  851. ecoff_data (abfd)->debug_info.symbolic_header.ifdMax);
  852. }
  853. ecoff_data (abfd)->canonical_symbols = internal;
  854. return TRUE;
  855. }
  856. /* Return the amount of space needed for the canonical symbols. */
  857. long
  858. _bfd_ecoff_get_symtab_upper_bound (bfd *abfd)
  859. {
  860. if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
  861. &ecoff_data (abfd)->debug_info))
  862. return -1;
  863. if (bfd_get_symcount (abfd) == 0)
  864. return 0;
  865. return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
  866. }
  867. /* Get the canonical symbols. */
  868. long
  869. _bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  870. {
  871. unsigned int counter = 0;
  872. ecoff_symbol_type *symbase;
  873. ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
  874. if (! _bfd_ecoff_slurp_symbol_table (abfd))
  875. return -1;
  876. if (bfd_get_symcount (abfd) == 0)
  877. return 0;
  878. symbase = ecoff_data (abfd)->canonical_symbols;
  879. while (counter < bfd_get_symcount (abfd))
  880. {
  881. *(location++) = symbase++;
  882. counter++;
  883. }
  884. *location++ = NULL;
  885. return bfd_get_symcount (abfd);
  886. }
  887. /* Turn ECOFF type information into a printable string.
  888. ecoff_emit_aggregate and ecoff_type_to_string are from
  889. gcc/mips-tdump.c, with swapping added and used_ptr removed. */
  890. /* Write aggregate information to a string. */
  891. static void
  892. ecoff_emit_aggregate (bfd *abfd,
  893. FDR *fdr,
  894. char *string,
  895. RNDXR *rndx,
  896. long isym,
  897. const char *which)
  898. {
  899. const struct ecoff_debug_swap * const debug_swap =
  900. &ecoff_backend (abfd)->debug_swap;
  901. struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
  902. unsigned int ifd = rndx->rfd;
  903. unsigned int indx = rndx->index;
  904. const char *name;
  905. if (ifd == 0xfff)
  906. ifd = isym;
  907. /* An ifd of -1 is an opaque type. An escaped index of 0 is a
  908. struct return type of a procedure compiled without -g. */
  909. if (ifd == 0xffffffff
  910. || (rndx->rfd == 0xfff && indx == 0))
  911. name = "<undefined>";
  912. else if (indx == indexNil)
  913. name = "<no name>";
  914. else
  915. {
  916. SYMR sym;
  917. if (debug_info->external_rfd == NULL)
  918. fdr = debug_info->fdr + ifd;
  919. else
  920. {
  921. RFDT rfd;
  922. (*debug_swap->swap_rfd_in) (abfd,
  923. ((char *) debug_info->external_rfd
  924. + ((fdr->rfdBase + ifd)
  925. * debug_swap->external_rfd_size)),
  926. &rfd);
  927. fdr = debug_info->fdr + rfd;
  928. }
  929. indx += fdr->isymBase;
  930. (*debug_swap->swap_sym_in) (abfd,
  931. ((char *) debug_info->external_sym
  932. + indx * debug_swap->external_sym_size),
  933. &sym);
  934. name = debug_info->ss + fdr->issBase + sym.iss;
  935. }
  936. sprintf (string,
  937. "%s %s { ifd = %u, index = %lu }",
  938. which, name, ifd,
  939. ((unsigned long) indx
  940. + debug_info->symbolic_header.iextMax));
  941. }
  942. /* Convert the type information to string format. */
  943. static char *
  944. ecoff_type_to_string (bfd *abfd, FDR *fdr, unsigned int indx)
  945. {
  946. union aux_ext *aux_ptr;
  947. int bigendian;
  948. AUXU u;
  949. struct qual
  950. {
  951. unsigned int type;
  952. int low_bound;
  953. int high_bound;
  954. int stride;
  955. } qualifiers[7];
  956. unsigned int basic_type;
  957. int i;
  958. char buffer1[1024];
  959. static char buffer2[1024];
  960. char *p1 = buffer1;
  961. char *p2 = buffer2;
  962. RNDXR rndx;
  963. aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
  964. bigendian = fdr->fBigendian;
  965. for (i = 0; i < 7; i++)
  966. {
  967. qualifiers[i].low_bound = 0;
  968. qualifiers[i].high_bound = 0;
  969. qualifiers[i].stride = 0;
  970. }
  971. if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
  972. return "-1 (no type)";
  973. _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
  974. basic_type = u.ti.bt;
  975. qualifiers[0].type = u.ti.tq0;
  976. qualifiers[1].type = u.ti.tq1;
  977. qualifiers[2].type = u.ti.tq2;
  978. qualifiers[3].type = u.ti.tq3;
  979. qualifiers[4].type = u.ti.tq4;
  980. qualifiers[5].type = u.ti.tq5;
  981. qualifiers[6].type = tqNil;
  982. /* Go get the basic type. */
  983. switch (basic_type)
  984. {
  985. case btNil: /* Undefined. */
  986. strcpy (p1, "nil");
  987. break;
  988. case btAdr: /* Address - integer same size as pointer. */
  989. strcpy (p1, "address");
  990. break;
  991. case btChar: /* Character. */
  992. strcpy (p1, "char");
  993. break;
  994. case btUChar: /* Unsigned character. */
  995. strcpy (p1, "unsigned char");
  996. break;
  997. case btShort: /* Short. */
  998. strcpy (p1, "short");
  999. break;
  1000. case btUShort: /* Unsigned short. */
  1001. strcpy (p1, "unsigned short");
  1002. break;
  1003. case btInt: /* Int. */
  1004. strcpy (p1, "int");
  1005. break;
  1006. case btUInt: /* Unsigned int. */
  1007. strcpy (p1, "unsigned int");
  1008. break;
  1009. case btLong: /* Long. */
  1010. strcpy (p1, "long");
  1011. break;
  1012. case btULong: /* Unsigned long. */
  1013. strcpy (p1, "unsigned long");
  1014. break;
  1015. case btFloat: /* Float (real). */
  1016. strcpy (p1, "float");
  1017. break;
  1018. case btDouble: /* Double (real). */
  1019. strcpy (p1, "double");
  1020. break;
  1021. /* Structures add 1-2 aux words:
  1022. 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
  1023. 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
  1024. case btStruct: /* Structure (Record). */
  1025. _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1026. ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
  1027. (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1028. "struct");
  1029. indx++; /* Skip aux words. */
  1030. break;
  1031. /* Unions add 1-2 aux words:
  1032. 1st word is [ST_RFDESCAPE, offset] pointer to union def;
  1033. 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
  1034. case btUnion: /* Union. */
  1035. _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1036. ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
  1037. (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1038. "union");
  1039. indx++; /* Skip aux words. */
  1040. break;
  1041. /* Enumerations add 1-2 aux words:
  1042. 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
  1043. 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
  1044. case btEnum: /* Enumeration. */
  1045. _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1046. ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
  1047. (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1048. "enum");
  1049. indx++; /* Skip aux words. */
  1050. break;
  1051. case btTypedef: /* Defined via a typedef, isymRef points. */
  1052. strcpy (p1, "typedef");
  1053. break;
  1054. case btRange: /* Subrange of int. */
  1055. strcpy (p1, "subrange");
  1056. break;
  1057. case btSet: /* Pascal sets. */
  1058. strcpy (p1, "set");
  1059. break;
  1060. case btComplex: /* Fortran complex. */
  1061. strcpy (p1, "complex");
  1062. break;
  1063. case btDComplex: /* Fortran double complex. */
  1064. strcpy (p1, "double complex");
  1065. break;
  1066. case btIndirect: /* Forward or unnamed typedef. */
  1067. strcpy (p1, "forward/unamed typedef");
  1068. break;
  1069. case btFixedDec: /* Fixed Decimal. */
  1070. strcpy (p1, "fixed decimal");
  1071. break;
  1072. case btFloatDec: /* Float Decimal. */
  1073. strcpy (p1, "float decimal");
  1074. break;
  1075. case btString: /* Varying Length Character String. */
  1076. strcpy (p1, "string");
  1077. break;
  1078. case btBit: /* Aligned Bit String. */
  1079. strcpy (p1, "bit");
  1080. break;
  1081. case btPicture: /* Picture. */
  1082. strcpy (p1, "picture");
  1083. break;
  1084. case btVoid: /* Void. */
  1085. strcpy (p1, "void");
  1086. break;
  1087. default:
  1088. sprintf (p1, _("Unknown basic type %d"), (int) basic_type);
  1089. break;
  1090. }
  1091. p1 += strlen (buffer1);
  1092. /* If this is a bitfield, get the bitsize. */
  1093. if (u.ti.fBitfield)
  1094. {
  1095. int bitsize;
  1096. bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
  1097. sprintf (p1, " : %d", bitsize);
  1098. p1 += strlen (buffer1);
  1099. }
  1100. /* Deal with any qualifiers. */
  1101. if (qualifiers[0].type != tqNil)
  1102. {
  1103. /* Snarf up any array bounds in the correct order. Arrays
  1104. store 5 successive words in the aux. table:
  1105. word 0 RNDXR to type of the bounds (ie, int)
  1106. word 1 Current file descriptor index
  1107. word 2 low bound
  1108. word 3 high bound (or -1 if [])
  1109. word 4 stride size in bits. */
  1110. for (i = 0; i < 7; i++)
  1111. {
  1112. if (qualifiers[i].type == tqArray)
  1113. {
  1114. qualifiers[i].low_bound =
  1115. AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
  1116. qualifiers[i].high_bound =
  1117. AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
  1118. qualifiers[i].stride =
  1119. AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
  1120. indx += 5;
  1121. }
  1122. }
  1123. /* Now print out the qualifiers. */
  1124. for (i = 0; i < 6; i++)
  1125. {
  1126. switch (qualifiers[i].type)
  1127. {
  1128. case tqNil:
  1129. case tqMax:
  1130. break;
  1131. case tqPtr:
  1132. strcpy (p2, "ptr to ");
  1133. p2 += sizeof ("ptr to ")-1;
  1134. break;
  1135. case tqVol:
  1136. strcpy (p2, "volatile ");
  1137. p2 += sizeof ("volatile ")-1;
  1138. break;
  1139. case tqFar:
  1140. strcpy (p2, "far ");
  1141. p2 += sizeof ("far ")-1;
  1142. break;
  1143. case tqProc:
  1144. strcpy (p2, "func. ret. ");
  1145. p2 += sizeof ("func. ret. ");
  1146. break;
  1147. case tqArray:
  1148. {
  1149. int first_array = i;
  1150. int j;
  1151. /* Print array bounds reversed (ie, in the order the C
  1152. programmer writes them). C is such a fun language.... */
  1153. while (i < 5 && qualifiers[i+1].type == tqArray)
  1154. i++;
  1155. for (j = i; j >= first_array; j--)
  1156. {
  1157. strcpy (p2, "array [");
  1158. p2 += sizeof ("array [")-1;
  1159. if (qualifiers[j].low_bound != 0)
  1160. sprintf (p2,
  1161. "%ld:%ld {%ld bits}",
  1162. (long) qualifiers[j].low_bound,
  1163. (long) qualifiers[j].high_bound,
  1164. (long) qualifiers[j].stride);
  1165. else if (qualifiers[j].high_bound != -1)
  1166. sprintf (p2,
  1167. "%ld {%ld bits}",
  1168. (long) (qualifiers[j].high_bound + 1),
  1169. (long) (qualifiers[j].stride));
  1170. else
  1171. sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
  1172. p2 += strlen (p2);
  1173. strcpy (p2, "] of ");
  1174. p2 += sizeof ("] of ")-1;
  1175. }
  1176. }
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. strcpy (p2, buffer1);
  1182. return buffer2;
  1183. }
  1184. /* Return information about ECOFF symbol SYMBOL in RET. */
  1185. void
  1186. _bfd_ecoff_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
  1187. asymbol *symbol,
  1188. symbol_info *ret)
  1189. {
  1190. bfd_symbol_info (symbol, ret);
  1191. }
  1192. /* Return whether this is a local label. */
  1193. bfd_boolean
  1194. _bfd_ecoff_bfd_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
  1195. const char *name)
  1196. {
  1197. return name[0] == '$';
  1198. }
  1199. /* Print information about an ECOFF symbol. */
  1200. void
  1201. _bfd_ecoff_print_symbol (bfd *abfd,
  1202. void * filep,
  1203. asymbol *symbol,
  1204. bfd_print_symbol_type how)
  1205. {
  1206. const struct ecoff_debug_swap * const debug_swap
  1207. = &ecoff_backend (abfd)->debug_swap;
  1208. FILE *file = (FILE *)filep;
  1209. switch (how)
  1210. {
  1211. case bfd_print_symbol_name:
  1212. fprintf (file, "%s", symbol->name);
  1213. break;
  1214. case bfd_print_symbol_more:
  1215. if (ecoffsymbol (symbol)->local)
  1216. {
  1217. SYMR ecoff_sym;
  1218. (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
  1219. &ecoff_sym);
  1220. fprintf (file, "ecoff local ");
  1221. fprintf_vma (file, (bfd_vma) ecoff_sym.value);
  1222. fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
  1223. (unsigned) ecoff_sym.sc);
  1224. }
  1225. else
  1226. {
  1227. EXTR ecoff_ext;
  1228. (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
  1229. &ecoff_ext);
  1230. fprintf (file, "ecoff extern ");
  1231. fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
  1232. fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
  1233. (unsigned) ecoff_ext.asym.sc);
  1234. }
  1235. break;
  1236. case bfd_print_symbol_all:
  1237. /* Print out the symbols in a reasonable way. */
  1238. {
  1239. char type;
  1240. int pos;
  1241. EXTR ecoff_ext;
  1242. char jmptbl;
  1243. char cobol_main;
  1244. char weakext;
  1245. if (ecoffsymbol (symbol)->local)
  1246. {
  1247. (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
  1248. &ecoff_ext.asym);
  1249. type = 'l';
  1250. pos = ((((char *) ecoffsymbol (symbol)->native
  1251. - (char *) ecoff_data (abfd)->debug_info.external_sym)
  1252. / debug_swap->external_sym_size)
  1253. + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
  1254. jmptbl = ' ';
  1255. cobol_main = ' ';
  1256. weakext = ' ';
  1257. }
  1258. else
  1259. {
  1260. (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
  1261. &ecoff_ext);
  1262. type = 'e';
  1263. pos = (((char *) ecoffsymbol (symbol)->native
  1264. - (char *) ecoff_data (abfd)->debug_info.external_ext)
  1265. / debug_swap->external_ext_size);
  1266. jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
  1267. cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
  1268. weakext = ecoff_ext.weakext ? 'w' : ' ';
  1269. }
  1270. fprintf (file, "[%3d] %c ",
  1271. pos, type);
  1272. fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
  1273. fprintf (file, " st %x sc %x indx %x %c%c%c %s",
  1274. (unsigned) ecoff_ext.asym.st,
  1275. (unsigned) ecoff_ext.asym.sc,
  1276. (unsigned) ecoff_ext.asym.index,
  1277. jmptbl, cobol_main, weakext,
  1278. symbol->name);
  1279. if (ecoffsymbol (symbol)->fdr != NULL
  1280. && ecoff_ext.asym.index != indexNil)
  1281. {
  1282. FDR *fdr;
  1283. unsigned int indx;
  1284. int bigendian;
  1285. bfd_size_type sym_base;
  1286. union aux_ext *aux_base;
  1287. fdr = ecoffsymbol (symbol)->fdr;
  1288. indx = ecoff_ext.asym.index;
  1289. /* sym_base is used to map the fdr relative indices which
  1290. appear in the file to the position number which we are
  1291. using. */
  1292. sym_base = fdr->isymBase;
  1293. if (ecoffsymbol (symbol)->local)
  1294. sym_base +=
  1295. ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
  1296. /* aux_base is the start of the aux entries for this file;
  1297. asym.index is an offset from this. */
  1298. aux_base = (ecoff_data (abfd)->debug_info.external_aux
  1299. + fdr->iauxBase);
  1300. /* The aux entries are stored in host byte order; the
  1301. order is indicated by a bit in the fdr. */
  1302. bigendian = fdr->fBigendian;
  1303. /* This switch is basically from gcc/mips-tdump.c. */
  1304. switch (ecoff_ext.asym.st)
  1305. {
  1306. case stNil:
  1307. case stLabel:
  1308. break;
  1309. case stFile:
  1310. case stBlock:
  1311. fprintf (file, _("\n End+1 symbol: %ld"),
  1312. (long) (indx + sym_base));
  1313. break;
  1314. case stEnd:
  1315. if (ecoff_ext.asym.sc == scText
  1316. || ecoff_ext.asym.sc == scInfo)
  1317. fprintf (file, _("\n First symbol: %ld"),
  1318. (long) (indx + sym_base));
  1319. else
  1320. fprintf (file, _("\n First symbol: %ld"),
  1321. ((long)
  1322. (AUX_GET_ISYM (bigendian,
  1323. &aux_base[ecoff_ext.asym.index])
  1324. + sym_base)));
  1325. break;
  1326. case stProc:
  1327. case stStaticProc:
  1328. if (ECOFF_IS_STAB (&ecoff_ext.asym))
  1329. ;
  1330. else if (ecoffsymbol (symbol)->local)
  1331. fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"),
  1332. ((long)
  1333. (AUX_GET_ISYM (bigendian,
  1334. &aux_base[ecoff_ext.asym.index])
  1335. + sym_base)),
  1336. ecoff_type_to_string (abfd, fdr, indx + 1));
  1337. else
  1338. fprintf (file, _("\n Local symbol: %ld"),
  1339. ((long) indx
  1340. + (long) sym_base
  1341. + (ecoff_data (abfd)
  1342. ->debug_info.symbolic_header.iextMax)));
  1343. break;
  1344. case stStruct:
  1345. fprintf (file, _("\n struct; End+1 symbol: %ld"),
  1346. (long) (indx + sym_base));
  1347. break;
  1348. case stUnion:
  1349. fprintf (file, _("\n union; End+1 symbol: %ld"),
  1350. (long) (indx + sym_base));
  1351. break;
  1352. case stEnum:
  1353. fprintf (file, _("\n enum; End+1 symbol: %ld"),
  1354. (long) (indx + sym_base));
  1355. break;
  1356. default:
  1357. if (! ECOFF_IS_STAB (&ecoff_ext.asym))
  1358. fprintf (file, _("\n Type: %s"),
  1359. ecoff_type_to_string (abfd, fdr, indx));
  1360. break;
  1361. }
  1362. }
  1363. }
  1364. break;
  1365. }
  1366. }
  1367. /* Read in the relocs for a section. */
  1368. static bfd_boolean
  1369. ecoff_slurp_reloc_table (bfd *abfd,
  1370. asection *section,
  1371. asymbol **symbols)
  1372. {
  1373. const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
  1374. arelent *internal_relocs;
  1375. bfd_size_type external_reloc_size;
  1376. bfd_size_type amt;
  1377. char *external_relocs;
  1378. arelent *rptr;
  1379. unsigned int i;
  1380. if (section->relocation != NULL
  1381. || section->reloc_count == 0
  1382. || (section->flags & SEC_CONSTRUCTOR) != 0)
  1383. return TRUE;
  1384. if (! _bfd_ecoff_slurp_symbol_table (abfd))
  1385. return FALSE;
  1386. amt = section->reloc_count;
  1387. amt *= sizeof (arelent);
  1388. internal_relocs = (arelent *) bfd_alloc (abfd, amt);
  1389. external_reloc_size = backend->external_reloc_size;
  1390. amt = external_reloc_size * section->reloc_count;
  1391. external_relocs = (char *) bfd_alloc (abfd, amt);
  1392. if (internal_relocs == NULL || external_relocs == NULL)
  1393. return FALSE;
  1394. if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
  1395. return FALSE;
  1396. if (bfd_bread (external_relocs, amt, abfd) != amt)
  1397. return FALSE;
  1398. for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
  1399. {
  1400. struct internal_reloc intern;
  1401. (*backend->swap_reloc_in) (abfd,
  1402. external_relocs + i * external_reloc_size,
  1403. &intern);
  1404. if (intern.r_extern)
  1405. {
  1406. /* r_symndx is an index into the external symbols. */
  1407. BFD_ASSERT (intern.r_symndx >= 0
  1408. && (intern.r_symndx
  1409. < (ecoff_data (abfd)
  1410. ->debug_info.symbolic_header.iextMax)));
  1411. rptr->sym_ptr_ptr = symbols + intern.r_symndx;
  1412. rptr->addend = 0;
  1413. }
  1414. else if (intern.r_symndx == RELOC_SECTION_NONE
  1415. || intern.r_symndx == RELOC_SECTION_ABS)
  1416. {
  1417. rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1418. rptr->addend = 0;
  1419. }
  1420. else
  1421. {
  1422. const char *sec_name;
  1423. asection *sec;
  1424. /* r_symndx is a section key. */
  1425. switch (intern.r_symndx)
  1426. {
  1427. case RELOC_SECTION_TEXT: sec_name = _TEXT; break;
  1428. case RELOC_SECTION_RDATA: sec_name = _RDATA; break;
  1429. case RELOC_SECTION_DATA: sec_name = _DATA; break;
  1430. case RELOC_SECTION_SDATA: sec_name = _SDATA; break;
  1431. case RELOC_SECTION_SBSS: sec_name = _SBSS; break;
  1432. case RELOC_SECTION_BSS: sec_name = _BSS; break;
  1433. case RELOC_SECTION_INIT: sec_name = _INIT; break;
  1434. case RELOC_SECTION_LIT8: sec_name = _LIT8; break;
  1435. case RELOC_SECTION_LIT4: sec_name = _LIT4; break;
  1436. case RELOC_SECTION_XDATA: sec_name = _XDATA; break;
  1437. case RELOC_SECTION_PDATA: sec_name = _PDATA; break;
  1438. case RELOC_SECTION_FINI: sec_name = _FINI; break;
  1439. case RELOC_SECTION_LITA: sec_name = _LITA; break;
  1440. case RELOC_SECTION_RCONST: sec_name = _RCONST; break;
  1441. default: abort ();
  1442. }
  1443. sec = bfd_get_section_by_name (abfd, sec_name);
  1444. if (sec == NULL)
  1445. abort ();
  1446. rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
  1447. rptr->addend = - bfd_get_section_vma (abfd, sec);
  1448. }
  1449. rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
  1450. /* Let the backend select the howto field and do any other
  1451. required processing. */
  1452. (*backend->adjust_reloc_in) (abfd, &intern, rptr);
  1453. }
  1454. bfd_release (abfd, external_relocs);
  1455. section->relocation = internal_relocs;
  1456. return TRUE;
  1457. }
  1458. /* Get a canonical list of relocs. */
  1459. long
  1460. _bfd_ecoff_canonicalize_reloc (bfd *abfd,
  1461. asection *section,
  1462. arelent **relptr,
  1463. asymbol **symbols)
  1464. {
  1465. unsigned int count;
  1466. if (section->flags & SEC_CONSTRUCTOR)
  1467. {
  1468. arelent_chain *chain;
  1469. /* This section has relocs made up by us, not the file, so take
  1470. them out of their chain and place them into the data area
  1471. provided. */
  1472. for (count = 0, chain = section->constructor_chain;
  1473. count < section->reloc_count;
  1474. count++, chain = chain->next)
  1475. *relptr++ = &chain->relent;
  1476. }
  1477. else
  1478. {
  1479. arelent *tblptr;
  1480. if (! ecoff_slurp_reloc_table (abfd, section, symbols))
  1481. return -1;
  1482. tblptr = section->relocation;
  1483. for (count = 0; count < section->reloc_count; count++)
  1484. *relptr++ = tblptr++;
  1485. }
  1486. *relptr = NULL;
  1487. return section->reloc_count;
  1488. }
  1489. /* Provided a BFD, a section and an offset into the section, calculate
  1490. and return the name of the source file and the line nearest to the
  1491. wanted location. */
  1492. bfd_boolean
  1493. _bfd_ecoff_find_nearest_line (bfd *abfd,
  1494. asymbol **symbols ATTRIBUTE_UNUSED,
  1495. asection *section,
  1496. bfd_vma offset,
  1497. const char **filename_ptr,
  1498. const char **functionname_ptr,
  1499. unsigned int *retline_ptr,
  1500. unsigned int *discriminator_ptr)
  1501. {
  1502. const struct ecoff_debug_swap * const debug_swap
  1503. = &ecoff_backend (abfd)->debug_swap;
  1504. struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
  1505. struct ecoff_find_line *line_info;
  1506. /* Make sure we have the FDR's. */
  1507. if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info)
  1508. || bfd_get_symcount (abfd) == 0)
  1509. return FALSE;
  1510. if (ecoff_data (abfd)->find_line_info == NULL)
  1511. {
  1512. bfd_size_type amt = sizeof (struct ecoff_find_line);
  1513. ecoff_data (abfd)->find_line_info =
  1514. (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
  1515. if (ecoff_data (abfd)->find_line_info == NULL)
  1516. return FALSE;
  1517. }
  1518. if (discriminator_ptr)
  1519. *discriminator_ptr = 0;
  1520. line_info = ecoff_data (abfd)->find_line_info;
  1521. return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
  1522. debug_swap, line_info, filename_ptr,
  1523. functionname_ptr, retline_ptr);
  1524. }
  1525. /* Copy private BFD data. This is called by objcopy and strip. We
  1526. use it to copy the ECOFF debugging information from one BFD to the
  1527. other. It would be theoretically possible to represent the ECOFF
  1528. debugging information in the symbol table. However, it would be a
  1529. lot of work, and there would be little gain (gas, gdb, and ld
  1530. already access the ECOFF debugging information via the
  1531. ecoff_debug_info structure, and that structure would have to be
  1532. retained in order to support ECOFF debugging in MIPS ELF).
  1533. The debugging information for the ECOFF external symbols comes from
  1534. the symbol table, so this function only handles the other debugging
  1535. information. */
  1536. bfd_boolean
  1537. _bfd_ecoff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  1538. {
  1539. struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
  1540. struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
  1541. int i;
  1542. asymbol **sym_ptr_ptr;
  1543. size_t c;
  1544. bfd_boolean local;
  1545. /* We only want to copy information over if both BFD's use ECOFF
  1546. format. */
  1547. if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
  1548. || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
  1549. return TRUE;
  1550. /* Copy the GP value and the register masks. */
  1551. ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
  1552. ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
  1553. ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
  1554. for (i = 0; i < 3; i++)
  1555. ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
  1556. /* Copy the version stamp. */
  1557. oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
  1558. /* If there are no symbols, don't copy any debugging information. */
  1559. c = bfd_get_symcount (obfd);
  1560. sym_ptr_ptr = bfd_get_outsymbols (obfd);
  1561. if (c == 0 || sym_ptr_ptr == NULL)
  1562. return TRUE;
  1563. /* See if there are any local symbols. */
  1564. local = FALSE;
  1565. for (; c > 0; c--, sym_ptr_ptr++)
  1566. {
  1567. if (ecoffsymbol (*sym_ptr_ptr)->local)
  1568. {
  1569. local = TRUE;
  1570. break;
  1571. }
  1572. }
  1573. if (local)
  1574. {
  1575. /* There are some local symbols. We just bring over all the
  1576. debugging information. FIXME: This is not quite the right
  1577. thing to do. If the user has asked us to discard all
  1578. debugging information, then we are probably going to wind up
  1579. keeping it because there will probably be some local symbol
  1580. which objcopy did not discard. We should actually break
  1581. apart the debugging information and only keep that which
  1582. applies to the symbols we want to keep. */
  1583. oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
  1584. oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
  1585. oinfo->line = iinfo->line;
  1586. oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
  1587. oinfo->external_dnr = iinfo->external_dnr;
  1588. oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
  1589. oinfo->external_pdr = iinfo->external_pdr;
  1590. oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
  1591. oinfo->external_sym = iinfo->external_sym;
  1592. oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
  1593. oinfo->external_opt = iinfo->external_opt;
  1594. oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
  1595. oinfo->external_aux = iinfo->external_aux;
  1596. oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
  1597. oinfo->ss = iinfo->ss;
  1598. oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
  1599. oinfo->external_fdr = iinfo->external_fdr;
  1600. oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
  1601. oinfo->external_rfd = iinfo->external_rfd;
  1602. }
  1603. else
  1604. {
  1605. /* We are discarding all the local symbol information. Look
  1606. through the external symbols and remove all references to FDR
  1607. or aux information. */
  1608. c = bfd_get_symcount (obfd);
  1609. sym_ptr_ptr = bfd_get_outsymbols (obfd);
  1610. for (; c > 0; c--, sym_ptr_ptr++)
  1611. {
  1612. EXTR esym;
  1613. (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
  1614. (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
  1615. esym.ifd = ifdNil;
  1616. esym.asym.index = indexNil;
  1617. (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
  1618. (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
  1619. }
  1620. }
  1621. return TRUE;
  1622. }
  1623. /* Set the architecture. The supported architecture is stored in the
  1624. backend pointer. We always set the architecture anyhow, since many
  1625. callers ignore the return value. */
  1626. bfd_boolean
  1627. _bfd_ecoff_set_arch_mach (bfd *abfd,
  1628. enum bfd_architecture arch,
  1629. unsigned long machine)
  1630. {
  1631. bfd_default_set_arch_mach (abfd, arch, machine);
  1632. return arch == ecoff_backend (abfd)->arch;
  1633. }
  1634. /* Get the size of the section headers. */
  1635. int
  1636. _bfd_ecoff_sizeof_headers (bfd *abfd,
  1637. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  1638. {
  1639. asection *current;
  1640. int c;
  1641. int ret;
  1642. c = 0;
  1643. for (current = abfd->sections;
  1644. current != NULL;
  1645. current = current->next)
  1646. ++c;
  1647. ret = (bfd_coff_filhsz (abfd)
  1648. + bfd_coff_aoutsz (abfd)
  1649. + c * bfd_coff_scnhsz (abfd));
  1650. return (int) BFD_ALIGN (ret, 16);
  1651. }
  1652. /* Get the contents of a section. */
  1653. bfd_boolean
  1654. _bfd_ecoff_get_section_contents (bfd *abfd,
  1655. asection *section,
  1656. void * location,
  1657. file_ptr offset,
  1658. bfd_size_type count)
  1659. {
  1660. return _bfd_generic_get_section_contents (abfd, section, location,
  1661. offset, count);
  1662. }
  1663. /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
  1664. called via qsort. */
  1665. static int
  1666. ecoff_sort_hdrs (const void * arg1, const void * arg2)
  1667. {
  1668. const asection *hdr1 = *(const asection **) arg1;
  1669. const asection *hdr2 = *(const asection **) arg2;
  1670. if ((hdr1->flags & SEC_ALLOC) != 0)
  1671. {
  1672. if ((hdr2->flags & SEC_ALLOC) == 0)
  1673. return -1;
  1674. }
  1675. else
  1676. {
  1677. if ((hdr2->flags & SEC_ALLOC) != 0)
  1678. return 1;
  1679. }
  1680. if (hdr1->vma < hdr2->vma)
  1681. return -1;
  1682. else if (hdr1->vma > hdr2->vma)
  1683. return 1;
  1684. else
  1685. return 0;
  1686. }
  1687. /* Calculate the file position for each section, and set
  1688. reloc_filepos. */
  1689. static bfd_boolean
  1690. ecoff_compute_section_file_positions (bfd *abfd)
  1691. {
  1692. file_ptr sofar, file_sofar;
  1693. asection **sorted_hdrs;
  1694. asection *current;
  1695. unsigned int i;
  1696. file_ptr old_sofar;
  1697. bfd_boolean rdata_in_text;
  1698. bfd_boolean first_data, first_nonalloc;
  1699. const bfd_vma round = ecoff_backend (abfd)->round;
  1700. bfd_size_type amt;
  1701. sofar = _bfd_ecoff_sizeof_headers (abfd, NULL);
  1702. file_sofar = sofar;
  1703. /* Sort the sections by VMA. */
  1704. amt = abfd->section_count;
  1705. amt *= sizeof (asection *);
  1706. sorted_hdrs = (asection **) bfd_malloc (amt);
  1707. if (sorted_hdrs == NULL)
  1708. return FALSE;
  1709. for (current = abfd->sections, i = 0;
  1710. current != NULL;
  1711. current = current->next, i++)
  1712. sorted_hdrs[i] = current;
  1713. BFD_ASSERT (i == abfd->section_count);
  1714. qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
  1715. ecoff_sort_hdrs);
  1716. /* Some versions of the OSF linker put the .rdata section in the
  1717. text segment, and some do not. */
  1718. rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
  1719. if (rdata_in_text)
  1720. {
  1721. for (i = 0; i < abfd->section_count; i++)
  1722. {
  1723. current = sorted_hdrs[i];
  1724. if (streq (current->name, _RDATA))
  1725. break;
  1726. if ((current->flags & SEC_CODE) == 0
  1727. && ! streq (current->name, _PDATA)
  1728. && ! streq (current->name, _RCONST))
  1729. {
  1730. rdata_in_text = FALSE;
  1731. break;
  1732. }
  1733. }
  1734. }
  1735. ecoff_data (abfd)->rdata_in_text = rdata_in_text;
  1736. first_data = TRUE;
  1737. first_nonalloc = TRUE;
  1738. for (i = 0; i < abfd->section_count; i++)
  1739. {
  1740. unsigned int alignment_power;
  1741. current = sorted_hdrs[i];
  1742. /* For the Alpha ECOFF .pdata section the lnnoptr field is
  1743. supposed to indicate the number of .pdata entries that are
  1744. really in the section. Each entry is 8 bytes. We store this
  1745. away in line_filepos before increasing the section size. */
  1746. if (streq (current->name, _PDATA))
  1747. current->line_filepos = current->size / 8;
  1748. alignment_power = current->alignment_power;
  1749. /* On Ultrix, the data sections in an executable file must be
  1750. aligned to a page boundary within the file. This does not
  1751. affect the section size, though. FIXME: Does this work for
  1752. other platforms? It requires some modification for the
  1753. Alpha, because .rdata on the Alpha goes with the text, not
  1754. the data. */
  1755. if ((abfd->flags & EXEC_P) != 0
  1756. && (abfd->flags & D_PAGED) != 0
  1757. && ! first_data
  1758. && (current->flags & SEC_CODE) == 0
  1759. && (! rdata_in_text
  1760. || ! streq (current->name, _RDATA))
  1761. && ! streq (current->name, _PDATA)
  1762. && ! streq (current->name, _RCONST))
  1763. {
  1764. sofar = (sofar + round - 1) &~ (round - 1);
  1765. file_sofar = (file_sofar + round - 1) &~ (round - 1);
  1766. first_data = FALSE;
  1767. }
  1768. else if (streq (current->name, _LIB))
  1769. {
  1770. /* On Irix 4, the location of contents of the .lib section
  1771. from a shared library section is also rounded up to a
  1772. page boundary. */
  1773. sofar = (sofar + round - 1) &~ (round - 1);
  1774. file_sofar = (file_sofar + round - 1) &~ (round - 1);
  1775. }
  1776. else if (first_nonalloc
  1777. && (current->flags & SEC_ALLOC) == 0
  1778. && (abfd->flags & D_PAGED) != 0)
  1779. {
  1780. /* Skip up to the next page for an unallocated section, such
  1781. as the .comment section on the Alpha. This leaves room
  1782. for the .bss section. */
  1783. first_nonalloc = FALSE;
  1784. sofar = (sofar + round - 1) &~ (round - 1);
  1785. file_sofar = (file_sofar + round - 1) &~ (round - 1);
  1786. }
  1787. /* Align the sections in the file to the same boundary on
  1788. which they are aligned in virtual memory. */
  1789. sofar = BFD_ALIGN (sofar, 1 << alignment_power);
  1790. if ((current->flags & SEC_HAS_CONTENTS) != 0)
  1791. file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
  1792. if ((abfd->flags & D_PAGED) != 0
  1793. && (current->flags & SEC_ALLOC) != 0)
  1794. {
  1795. sofar += (current->vma - sofar) % round;
  1796. if ((current->flags & SEC_HAS_CONTENTS) != 0)
  1797. file_sofar += (current->vma - file_sofar) % round;
  1798. }
  1799. if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
  1800. current->filepos = file_sofar;
  1801. sofar += current->size;
  1802. if ((current->flags & SEC_HAS_CONTENTS) != 0)
  1803. file_sofar += current->size;
  1804. /* Make sure that this section is of the right size too. */
  1805. old_sofar = sofar;
  1806. sofar = BFD_ALIGN (sofar, 1 << alignment_power);
  1807. if ((current->flags & SEC_HAS_CONTENTS) != 0)
  1808. file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
  1809. current->size += sofar - old_sofar;
  1810. }
  1811. free (sorted_hdrs);
  1812. sorted_hdrs = NULL;
  1813. ecoff_data (abfd)->reloc_filepos = file_sofar;
  1814. return TRUE;
  1815. }
  1816. /* Determine the location of the relocs for all the sections in the
  1817. output file, as well as the location of the symbolic debugging
  1818. information. */
  1819. static bfd_size_type
  1820. ecoff_compute_reloc_file_positions (bfd *abfd)
  1821. {
  1822. const bfd_size_type external_reloc_size =
  1823. ecoff_backend (abfd)->external_reloc_size;
  1824. file_ptr reloc_base;
  1825. bfd_size_type reloc_size;
  1826. asection *current;
  1827. file_ptr sym_base;
  1828. if (! abfd->output_has_begun)
  1829. {
  1830. if (! ecoff_compute_section_file_positions (abfd))
  1831. abort ();
  1832. abfd->output_has_begun = TRUE;
  1833. }
  1834. reloc_base = ecoff_data (abfd)->reloc_filepos;
  1835. reloc_size = 0;
  1836. for (current = abfd->sections;
  1837. current != NULL;
  1838. current = current->next)
  1839. {
  1840. if (current->reloc_count == 0)
  1841. current->rel_filepos = 0;
  1842. else
  1843. {
  1844. bfd_size_type relsize;
  1845. current->rel_filepos = reloc_base;
  1846. relsize = current->reloc_count * external_reloc_size;
  1847. reloc_size += relsize;
  1848. reloc_base += relsize;
  1849. }
  1850. }
  1851. sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
  1852. /* At least on Ultrix, the symbol table of an executable file must
  1853. be aligned to a page boundary. FIXME: Is this true on other
  1854. platforms? */
  1855. if ((abfd->flags & EXEC_P) != 0
  1856. && (abfd->flags & D_PAGED) != 0)
  1857. sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
  1858. &~ (ecoff_backend (abfd)->round - 1));
  1859. ecoff_data (abfd)->sym_filepos = sym_base;
  1860. return reloc_size;
  1861. }
  1862. /* Set the contents of a section. */
  1863. bfd_boolean
  1864. _bfd_ecoff_set_section_contents (bfd *abfd,
  1865. asection *section,
  1866. const void * location,
  1867. file_ptr offset,
  1868. bfd_size_type count)
  1869. {
  1870. file_ptr pos;
  1871. /* This must be done first, because bfd_set_section_contents is
  1872. going to set output_has_begun to TRUE. */
  1873. if (! abfd->output_has_begun
  1874. && ! ecoff_compute_section_file_positions (abfd))
  1875. return FALSE;
  1876. /* Handle the .lib section specially so that Irix 4 shared libraries
  1877. work out. See coff_set_section_contents in coffcode.h. */
  1878. if (streq (section->name, _LIB))
  1879. {
  1880. bfd_byte *rec, *recend;
  1881. rec = (bfd_byte *) location;
  1882. recend = rec + count;
  1883. while (rec < recend)
  1884. {
  1885. ++section->lma;
  1886. rec += bfd_get_32 (abfd, rec) * 4;
  1887. }
  1888. BFD_ASSERT (rec == recend);
  1889. }
  1890. if (count == 0)
  1891. return TRUE;
  1892. pos = section->filepos + offset;
  1893. if (bfd_seek (abfd, pos, SEEK_SET) != 0
  1894. || bfd_bwrite (location, count, abfd) != count)
  1895. return FALSE;
  1896. return TRUE;
  1897. }
  1898. /* Get the GP value for an ECOFF file. This is a hook used by
  1899. nlmconv. */
  1900. bfd_vma
  1901. bfd_ecoff_get_gp_value (bfd *abfd)
  1902. {
  1903. if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
  1904. || bfd_get_format (abfd) != bfd_object)
  1905. {
  1906. bfd_set_error (bfd_error_invalid_operation);
  1907. return 0;
  1908. }
  1909. return ecoff_data (abfd)->gp;
  1910. }
  1911. /* Set the GP value for an ECOFF file. This is a hook used by the
  1912. assembler. */
  1913. bfd_boolean
  1914. bfd_ecoff_set_gp_value (bfd *abfd, bfd_vma gp_value)
  1915. {
  1916. if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
  1917. || bfd_get_format (abfd) != bfd_object)
  1918. {
  1919. bfd_set_error (bfd_error_invalid_operation);
  1920. return FALSE;
  1921. }
  1922. ecoff_data (abfd)->gp = gp_value;
  1923. return TRUE;
  1924. }
  1925. /* Set the register masks for an ECOFF file. This is a hook used by
  1926. the assembler. */
  1927. bfd_boolean
  1928. bfd_ecoff_set_regmasks (bfd *abfd,
  1929. unsigned long gprmask,
  1930. unsigned long fprmask,
  1931. unsigned long *cprmask)
  1932. {
  1933. ecoff_data_type *tdata;
  1934. if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
  1935. || bfd_get_format (abfd) != bfd_object)
  1936. {
  1937. bfd_set_error (bfd_error_invalid_operation);
  1938. return FALSE;
  1939. }
  1940. tdata = ecoff_data (abfd);
  1941. tdata->gprmask = gprmask;
  1942. tdata->fprmask = fprmask;
  1943. if (cprmask != NULL)
  1944. {
  1945. int i;
  1946. for (i = 0; i < 3; i++)
  1947. tdata->cprmask[i] = cprmask[i];
  1948. }
  1949. return TRUE;
  1950. }
  1951. /* Get ECOFF EXTR information for an external symbol. This function
  1952. is passed to bfd_ecoff_debug_externals. */
  1953. static bfd_boolean
  1954. ecoff_get_extr (asymbol *sym, EXTR *esym)
  1955. {
  1956. ecoff_symbol_type *ecoff_sym_ptr;
  1957. bfd *input_bfd;
  1958. if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
  1959. || ecoffsymbol (sym)->native == NULL)
  1960. {
  1961. /* Don't include debugging, local, or section symbols. */
  1962. if ((sym->flags & BSF_DEBUGGING) != 0
  1963. || (sym->flags & BSF_LOCAL) != 0
  1964. || (sym->flags & BSF_SECTION_SYM) != 0)
  1965. return FALSE;
  1966. esym->jmptbl = 0;
  1967. esym->cobol_main = 0;
  1968. esym->weakext = (sym->flags & BSF_WEAK) != 0;
  1969. esym->reserved = 0;
  1970. esym->ifd = ifdNil;
  1971. /* FIXME: we can do better than this for st and sc. */
  1972. esym->asym.st = stGlobal;
  1973. esym->asym.sc = scAbs;
  1974. esym->asym.reserved = 0;
  1975. esym->asym.index = indexNil;
  1976. return TRUE;
  1977. }
  1978. ecoff_sym_ptr = ecoffsymbol (sym);
  1979. if (ecoff_sym_ptr->local)
  1980. return FALSE;
  1981. input_bfd = bfd_asymbol_bfd (sym);
  1982. (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
  1983. (input_bfd, ecoff_sym_ptr->native, esym);
  1984. /* If the symbol was defined by the linker, then esym will be
  1985. undefined but sym will not be. Get a better class for such a
  1986. symbol. */
  1987. if ((esym->asym.sc == scUndefined
  1988. || esym->asym.sc == scSUndefined)
  1989. && ! bfd_is_und_section (bfd_get_section (sym)))
  1990. esym->asym.sc = scAbs;
  1991. /* Adjust the FDR index for the symbol by that used for the input
  1992. BFD. */
  1993. if (esym->ifd != -1)
  1994. {
  1995. struct ecoff_debug_info *input_debug;
  1996. input_debug = &ecoff_data (input_bfd)->debug_info;
  1997. BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
  1998. if (input_debug->ifdmap != NULL)
  1999. esym->ifd = input_debug->ifdmap[esym->ifd];
  2000. }
  2001. return TRUE;
  2002. }
  2003. /* Set the external symbol index. This routine is passed to
  2004. bfd_ecoff_debug_externals. */
  2005. static void
  2006. ecoff_set_index (asymbol *sym, bfd_size_type indx)
  2007. {
  2008. ecoff_set_sym_index (sym, indx);
  2009. }
  2010. /* Write out an ECOFF file. */
  2011. bfd_boolean
  2012. _bfd_ecoff_write_object_contents (bfd *abfd)
  2013. {
  2014. const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
  2015. const bfd_vma round = backend->round;
  2016. const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
  2017. const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
  2018. const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
  2019. const bfd_size_type external_hdr_size
  2020. = backend->debug_swap.external_hdr_size;
  2021. const bfd_size_type external_reloc_size = backend->external_reloc_size;
  2022. void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *)
  2023. = backend->adjust_reloc_out;
  2024. void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *)
  2025. = backend->swap_reloc_out;
  2026. struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
  2027. HDRR * const symhdr = &debug->symbolic_header;
  2028. asection *current;
  2029. unsigned int count;
  2030. bfd_size_type reloc_size;
  2031. bfd_size_type text_size;
  2032. bfd_vma text_start;
  2033. bfd_boolean set_text_start;
  2034. bfd_size_type data_size;
  2035. bfd_vma data_start;
  2036. bfd_boolean set_data_start;
  2037. bfd_size_type bss_size;
  2038. void * buff = NULL;
  2039. void * reloc_buff = NULL;
  2040. struct internal_filehdr internal_f;
  2041. struct internal_aouthdr internal_a;
  2042. int i;
  2043. /* Determine where the sections and relocs will go in the output
  2044. file. */
  2045. reloc_size = ecoff_compute_reloc_file_positions (abfd);
  2046. count = 1;
  2047. for (current = abfd->sections;
  2048. current != NULL;
  2049. current = current->next)
  2050. {
  2051. current->target_index = count;
  2052. ++count;
  2053. }
  2054. if ((abfd->flags & D_PAGED) != 0)
  2055. text_size = _bfd_ecoff_sizeof_headers (abfd, NULL);
  2056. else
  2057. text_size = 0;
  2058. text_start = 0;
  2059. set_text_start = FALSE;
  2060. data_size = 0;
  2061. data_start = 0;
  2062. set_data_start = FALSE;
  2063. bss_size = 0;
  2064. /* Write section headers to the file. */
  2065. /* Allocate buff big enough to hold a section header,
  2066. file header, or a.out header. */
  2067. {
  2068. bfd_size_type siz;
  2069. siz = scnhsz;
  2070. if (siz < filhsz)
  2071. siz = filhsz;
  2072. if (siz < aoutsz)
  2073. siz = aoutsz;
  2074. buff = bfd_malloc (siz);
  2075. if (buff == NULL)
  2076. goto error_return;
  2077. }
  2078. internal_f.f_nscns = 0;
  2079. if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
  2080. goto error_return;
  2081. for (current = abfd->sections;
  2082. current != NULL;
  2083. current = current->next)
  2084. {
  2085. struct internal_scnhdr section;
  2086. bfd_vma vma;
  2087. ++internal_f.f_nscns;
  2088. strncpy (section.s_name, current->name, sizeof section.s_name);
  2089. /* This seems to be correct for Irix 4 shared libraries. */
  2090. vma = bfd_get_section_vma (abfd, current);
  2091. if (streq (current->name, _LIB))
  2092. section.s_vaddr = 0;
  2093. else
  2094. section.s_vaddr = vma;
  2095. section.s_paddr = current->lma;
  2096. section.s_size = current->size;
  2097. /* If this section is unloadable then the scnptr will be 0. */
  2098. if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
  2099. section.s_scnptr = 0;
  2100. else
  2101. section.s_scnptr = current->filepos;
  2102. section.s_relptr = current->rel_filepos;
  2103. /* FIXME: the lnnoptr of the .sbss or .sdata section of an
  2104. object file produced by the assembler is supposed to point to
  2105. information about how much room is required by objects of
  2106. various different sizes. I think this only matters if we
  2107. want the linker to compute the best size to use, or
  2108. something. I don't know what happens if the information is
  2109. not present. */
  2110. if (! streq (current->name, _PDATA))
  2111. section.s_lnnoptr = 0;
  2112. else
  2113. {
  2114. /* The Alpha ECOFF .pdata section uses the lnnoptr field to
  2115. hold the number of entries in the section (each entry is
  2116. 8 bytes). We stored this in the line_filepos field in
  2117. ecoff_compute_section_file_positions. */
  2118. section.s_lnnoptr = current->line_filepos;
  2119. }
  2120. section.s_nreloc = current->reloc_count;
  2121. section.s_nlnno = 0;
  2122. section.s_flags = ecoff_sec_to_styp_flags (current->name,
  2123. current->flags);
  2124. if (bfd_coff_swap_scnhdr_out (abfd, (void *) &section, buff) == 0
  2125. || bfd_bwrite (buff, scnhsz, abfd) != scnhsz)
  2126. goto error_return;
  2127. if ((section.s_flags & STYP_TEXT) != 0
  2128. || ((section.s_flags & STYP_RDATA) != 0
  2129. && ecoff_data (abfd)->rdata_in_text)
  2130. || section.s_flags == STYP_PDATA
  2131. || (section.s_flags & STYP_DYNAMIC) != 0
  2132. || (section.s_flags & STYP_LIBLIST) != 0
  2133. || (section.s_flags & STYP_RELDYN) != 0
  2134. || section.s_flags == STYP_CONFLIC
  2135. || (section.s_flags & STYP_DYNSTR) != 0
  2136. || (section.s_flags & STYP_DYNSYM) != 0
  2137. || (section.s_flags & STYP_HASH) != 0
  2138. || (section.s_flags & STYP_ECOFF_INIT) != 0
  2139. || (section.s_flags & STYP_ECOFF_FINI) != 0
  2140. || section.s_flags == STYP_RCONST)
  2141. {
  2142. text_size += current->size;
  2143. if (! set_text_start || text_start > vma)
  2144. {
  2145. text_start = vma;
  2146. set_text_start = TRUE;
  2147. }
  2148. }
  2149. else if ((section.s_flags & STYP_RDATA) != 0
  2150. || (section.s_flags & STYP_DATA) != 0
  2151. || (section.s_flags & STYP_LITA) != 0
  2152. || (section.s_flags & STYP_LIT8) != 0
  2153. || (section.s_flags & STYP_LIT4) != 0
  2154. || (section.s_flags & STYP_SDATA) != 0
  2155. || section.s_flags == STYP_XDATA
  2156. || (section.s_flags & STYP_GOT) != 0)
  2157. {
  2158. data_size += current->size;
  2159. if (! set_data_start || data_start > vma)
  2160. {
  2161. data_start = vma;
  2162. set_data_start = TRUE;
  2163. }
  2164. }
  2165. else if ((section.s_flags & STYP_BSS) != 0
  2166. || (section.s_flags & STYP_SBSS) != 0)
  2167. bss_size += current->size;
  2168. else if (section.s_flags == 0
  2169. || (section.s_flags & STYP_ECOFF_LIB) != 0
  2170. || section.s_flags == STYP_COMMENT)
  2171. /* Do nothing. */ ;
  2172. else
  2173. abort ();
  2174. }
  2175. /* Set up the file header. */
  2176. internal_f.f_magic = ecoff_get_magic (abfd);
  2177. /* We will NOT put a fucking timestamp in the header here. Every
  2178. time you put it back, I will come in and take it out again. I'm
  2179. sorry. This field does not belong here. We fill it with a 0 so
  2180. it compares the same but is not a reasonable time. --
  2181. gnu@cygnus.com. */
  2182. internal_f.f_timdat = 0;
  2183. if (bfd_get_symcount (abfd) != 0)
  2184. {
  2185. /* The ECOFF f_nsyms field is not actually the number of
  2186. symbols, it's the size of symbolic information header. */
  2187. internal_f.f_nsyms = external_hdr_size;
  2188. internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
  2189. }
  2190. else
  2191. {
  2192. internal_f.f_nsyms = 0;
  2193. internal_f.f_symptr = 0;
  2194. }
  2195. internal_f.f_opthdr = aoutsz;
  2196. internal_f.f_flags = F_LNNO;
  2197. if (reloc_size == 0)
  2198. internal_f.f_flags |= F_RELFLG;
  2199. if (bfd_get_symcount (abfd) == 0)
  2200. internal_f.f_flags |= F_LSYMS;
  2201. if (abfd->flags & EXEC_P)
  2202. internal_f.f_flags |= F_EXEC;
  2203. if (bfd_little_endian (abfd))
  2204. internal_f.f_flags |= F_AR32WR;
  2205. else
  2206. internal_f.f_flags |= F_AR32W;
  2207. /* Set up the ``optional'' header. */
  2208. if ((abfd->flags & D_PAGED) != 0)
  2209. internal_a.magic = ECOFF_AOUT_ZMAGIC;
  2210. else
  2211. internal_a.magic = ECOFF_AOUT_OMAGIC;
  2212. /* FIXME: Is this really correct? */
  2213. internal_a.vstamp = symhdr->vstamp;
  2214. /* At least on Ultrix, these have to be rounded to page boundaries.
  2215. FIXME: Is this true on other platforms? */
  2216. if ((abfd->flags & D_PAGED) != 0)
  2217. {
  2218. internal_a.tsize = (text_size + round - 1) &~ (round - 1);
  2219. internal_a.text_start = text_start &~ (round - 1);
  2220. internal_a.dsize = (data_size + round - 1) &~ (round - 1);
  2221. internal_a.data_start = data_start &~ (round - 1);
  2222. }
  2223. else
  2224. {
  2225. internal_a.tsize = text_size;
  2226. internal_a.text_start = text_start;
  2227. internal_a.dsize = data_size;
  2228. internal_a.data_start = data_start;
  2229. }
  2230. /* On Ultrix, the initial portions of the .sbss and .bss segments
  2231. are at the end of the data section. The bsize field in the
  2232. optional header records how many bss bytes are required beyond
  2233. those in the data section. The value is not rounded to a page
  2234. boundary. */
  2235. if (bss_size < internal_a.dsize - data_size)
  2236. bss_size = 0;
  2237. else
  2238. bss_size -= internal_a.dsize - data_size;
  2239. internal_a.bsize = bss_size;
  2240. internal_a.bss_start = internal_a.data_start + internal_a.dsize;
  2241. internal_a.entry = bfd_get_start_address (abfd);
  2242. internal_a.gp_value = ecoff_data (abfd)->gp;
  2243. internal_a.gprmask = ecoff_data (abfd)->gprmask;
  2244. internal_a.fprmask = ecoff_data (abfd)->fprmask;
  2245. for (i = 0; i < 4; i++)
  2246. internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
  2247. /* Let the backend adjust the headers if necessary. */
  2248. if (backend->adjust_headers)
  2249. {
  2250. if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
  2251. goto error_return;
  2252. }
  2253. /* Write out the file header and the optional header. */
  2254. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  2255. goto error_return;
  2256. bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff);
  2257. if (bfd_bwrite (buff, filhsz, abfd) != filhsz)
  2258. goto error_return;
  2259. bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff);
  2260. if (bfd_bwrite (buff, aoutsz, abfd) != aoutsz)
  2261. goto error_return;
  2262. /* Build the external symbol information. This must be done before
  2263. writing out the relocs so that we know the symbol indices. We
  2264. don't do this if this BFD was created by the backend linker,
  2265. since it will have already handled the symbols and relocs. */
  2266. if (! ecoff_data (abfd)->linker)
  2267. {
  2268. symhdr->iextMax = 0;
  2269. symhdr->issExtMax = 0;
  2270. debug->external_ext = debug->external_ext_end = NULL;
  2271. debug->ssext = debug->ssext_end = NULL;
  2272. if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
  2273. (abfd->flags & EXEC_P) == 0,
  2274. ecoff_get_extr, ecoff_set_index))
  2275. goto error_return;
  2276. /* Write out the relocs. */
  2277. for (current = abfd->sections;
  2278. current != NULL;
  2279. current = current->next)
  2280. {
  2281. arelent **reloc_ptr_ptr;
  2282. arelent **reloc_end;
  2283. char *out_ptr;
  2284. bfd_size_type amt;
  2285. if (current->reloc_count == 0)
  2286. continue;
  2287. amt = current->reloc_count * external_reloc_size;
  2288. reloc_buff = bfd_alloc (abfd, amt);
  2289. if (reloc_buff == NULL)
  2290. goto error_return;
  2291. reloc_ptr_ptr = current->orelocation;
  2292. reloc_end = reloc_ptr_ptr + current->reloc_count;
  2293. out_ptr = (char *) reloc_buff;
  2294. for (;
  2295. reloc_ptr_ptr < reloc_end;
  2296. reloc_ptr_ptr++, out_ptr += external_reloc_size)
  2297. {
  2298. arelent *reloc;
  2299. asymbol *sym;
  2300. struct internal_reloc in;
  2301. memset ((void *) &in, 0, sizeof in);
  2302. reloc = *reloc_ptr_ptr;
  2303. sym = *reloc->sym_ptr_ptr;
  2304. /* If the howto field has not been initialised then skip this reloc.
  2305. This assumes that an error message has been issued elsewhere. */
  2306. if (reloc->howto == NULL)
  2307. continue;
  2308. in.r_vaddr = (reloc->address
  2309. + bfd_get_section_vma (abfd, current));
  2310. in.r_type = reloc->howto->type;
  2311. if ((sym->flags & BSF_SECTION_SYM) == 0)
  2312. {
  2313. in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
  2314. in.r_extern = 1;
  2315. }
  2316. else
  2317. {
  2318. const char *name;
  2319. unsigned int j;
  2320. static struct
  2321. {
  2322. const char * name;
  2323. long r_symndx;
  2324. }
  2325. section_symndx [] =
  2326. {
  2327. { _TEXT, RELOC_SECTION_TEXT },
  2328. { _RDATA, RELOC_SECTION_RDATA },
  2329. { _DATA, RELOC_SECTION_DATA },
  2330. { _SDATA, RELOC_SECTION_SDATA },
  2331. { _SBSS, RELOC_SECTION_SBSS },
  2332. { _BSS, RELOC_SECTION_BSS },
  2333. { _INIT, RELOC_SECTION_INIT },
  2334. { _LIT8, RELOC_SECTION_LIT8 },
  2335. { _LIT4, RELOC_SECTION_LIT4 },
  2336. { _XDATA, RELOC_SECTION_XDATA },
  2337. { _PDATA, RELOC_SECTION_PDATA },
  2338. { _FINI, RELOC_SECTION_FINI },
  2339. { _LITA, RELOC_SECTION_LITA },
  2340. { "*ABS*", RELOC_SECTION_ABS },
  2341. { _RCONST, RELOC_SECTION_RCONST }
  2342. };
  2343. name = bfd_get_section_name (abfd, bfd_get_section (sym));
  2344. for (j = 0; j < ARRAY_SIZE (section_symndx); j++)
  2345. if (streq (name, section_symndx[j].name))
  2346. {
  2347. in.r_symndx = section_symndx[j].r_symndx;
  2348. break;
  2349. }
  2350. if (j == ARRAY_SIZE (section_symndx))
  2351. abort ();
  2352. in.r_extern = 0;
  2353. }
  2354. (*adjust_reloc_out) (abfd, reloc, &in);
  2355. (*swap_reloc_out) (abfd, &in, (void *) out_ptr);
  2356. }
  2357. if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
  2358. goto error_return;
  2359. amt = current->reloc_count * external_reloc_size;
  2360. if (bfd_bwrite (reloc_buff, amt, abfd) != amt)
  2361. goto error_return;
  2362. bfd_release (abfd, reloc_buff);
  2363. reloc_buff = NULL;
  2364. }
  2365. /* Write out the symbolic debugging information. */
  2366. if (bfd_get_symcount (abfd) > 0)
  2367. {
  2368. /* Write out the debugging information. */
  2369. if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
  2370. ecoff_data (abfd)->sym_filepos))
  2371. goto error_return;
  2372. }
  2373. }
  2374. /* The .bss section of a demand paged executable must receive an
  2375. entire page. If there are symbols, the symbols will start on the
  2376. next page. If there are no symbols, we must fill out the page by
  2377. hand. */
  2378. if (bfd_get_symcount (abfd) == 0
  2379. && (abfd->flags & EXEC_P) != 0
  2380. && (abfd->flags & D_PAGED) != 0)
  2381. {
  2382. char c;
  2383. if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
  2384. SEEK_SET) != 0)
  2385. goto error_return;
  2386. if (bfd_bread (&c, (bfd_size_type) 1, abfd) == 0)
  2387. c = 0;
  2388. if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
  2389. SEEK_SET) != 0)
  2390. goto error_return;
  2391. if (bfd_bwrite (&c, (bfd_size_type) 1, abfd) != 1)
  2392. goto error_return;
  2393. }
  2394. if (reloc_buff != NULL)
  2395. bfd_release (abfd, reloc_buff);
  2396. if (buff != NULL)
  2397. free (buff);
  2398. return TRUE;
  2399. error_return:
  2400. if (reloc_buff != NULL)
  2401. bfd_release (abfd, reloc_buff);
  2402. if (buff != NULL)
  2403. free (buff);
  2404. return FALSE;
  2405. }
  2406. /* Archive handling. ECOFF uses what appears to be a unique type of
  2407. archive header (armap). The byte ordering of the armap and the
  2408. contents are encoded in the name of the armap itself. At least for
  2409. now, we only support archives with the same byte ordering in the
  2410. armap and the contents.
  2411. The first four bytes in the armap are the number of symbol
  2412. definitions. This is always a power of two.
  2413. This is followed by the symbol definitions. Each symbol definition
  2414. occupies 8 bytes. The first four bytes are the offset from the
  2415. start of the armap strings to the null-terminated string naming
  2416. this symbol. The second four bytes are the file offset to the
  2417. archive member which defines this symbol. If the second four bytes
  2418. are 0, then this is not actually a symbol definition, and it should
  2419. be ignored.
  2420. The symbols are hashed into the armap with a closed hashing scheme.
  2421. See the functions below for the details of the algorithm.
  2422. After the symbol definitions comes four bytes holding the size of
  2423. the string table, followed by the string table itself. */
  2424. /* The name of an archive headers looks like this:
  2425. __________E[BL]E[BL]_ (with a trailing space).
  2426. The trailing space is changed to an X if the archive is changed to
  2427. indicate that the armap is out of date.
  2428. The Alpha seems to use ________64E[BL]E[BL]_. */
  2429. #define ARMAP_BIG_ENDIAN 'B'
  2430. #define ARMAP_LITTLE_ENDIAN 'L'
  2431. #define ARMAP_MARKER 'E'
  2432. #define ARMAP_START_LENGTH 10
  2433. #define ARMAP_HEADER_MARKER_INDEX 10
  2434. #define ARMAP_HEADER_ENDIAN_INDEX 11
  2435. #define ARMAP_OBJECT_MARKER_INDEX 12
  2436. #define ARMAP_OBJECT_ENDIAN_INDEX 13
  2437. #define ARMAP_END_INDEX 14
  2438. #define ARMAP_END "_ "
  2439. /* This is a magic number used in the hashing algorithm. */
  2440. #define ARMAP_HASH_MAGIC 0x9dd68ab5
  2441. /* This returns the hash value to use for a string. It also sets
  2442. *REHASH to the rehash adjustment if the first slot is taken. SIZE
  2443. is the number of entries in the hash table, and HLOG is the log
  2444. base 2 of SIZE. */
  2445. static unsigned int
  2446. ecoff_armap_hash (const char *s,
  2447. unsigned int *rehash,
  2448. unsigned int size,
  2449. unsigned int hlog)
  2450. {
  2451. unsigned int hash;
  2452. if (hlog == 0)
  2453. return 0;
  2454. hash = *s++;
  2455. while (*s != '\0')
  2456. hash = ((hash >> 27) | (hash << 5)) + *s++;
  2457. hash *= ARMAP_HASH_MAGIC;
  2458. *rehash = (hash & (size - 1)) | 1;
  2459. return hash >> (32 - hlog);
  2460. }
  2461. /* Read in the armap. */
  2462. bfd_boolean
  2463. _bfd_ecoff_slurp_armap (bfd *abfd)
  2464. {
  2465. char nextname[17];
  2466. unsigned int i;
  2467. struct areltdata *mapdata;
  2468. bfd_size_type parsed_size;
  2469. char *raw_armap;
  2470. struct artdata *ardata;
  2471. unsigned int count;
  2472. char *raw_ptr;
  2473. carsym *symdef_ptr;
  2474. char *stringbase;
  2475. bfd_size_type amt;
  2476. /* Get the name of the first element. */
  2477. i = bfd_bread ((void *) nextname, (bfd_size_type) 16, abfd);
  2478. if (i == 0)
  2479. return TRUE;
  2480. if (i != 16)
  2481. return FALSE;
  2482. if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
  2483. return FALSE;
  2484. /* Irix 4.0.5F apparently can use either an ECOFF armap or a
  2485. standard COFF armap. We could move the ECOFF armap stuff into
  2486. bfd_slurp_armap, but that seems inappropriate since no other
  2487. target uses this format. Instead, we check directly for a COFF
  2488. armap. */
  2489. if (CONST_STRNEQ (nextname, "/ "))
  2490. return bfd_slurp_armap (abfd);
  2491. /* See if the first element is an armap. */
  2492. if (! strneq (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH)
  2493. || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
  2494. || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
  2495. && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
  2496. || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
  2497. || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
  2498. && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
  2499. || ! strneq (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1))
  2500. {
  2501. bfd_has_map (abfd) = FALSE;
  2502. return TRUE;
  2503. }
  2504. /* Make sure we have the right byte ordering. */
  2505. if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
  2506. ^ (bfd_header_big_endian (abfd)))
  2507. || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
  2508. ^ (bfd_big_endian (abfd))))
  2509. {
  2510. bfd_set_error (bfd_error_wrong_format);
  2511. return FALSE;
  2512. }
  2513. /* Read in the armap. */
  2514. ardata = bfd_ardata (abfd);
  2515. mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
  2516. if (mapdata == NULL)
  2517. return FALSE;
  2518. parsed_size = mapdata->parsed_size;
  2519. free (mapdata);
  2520. raw_armap = (char *) bfd_alloc (abfd, parsed_size);
  2521. if (raw_armap == NULL)
  2522. return FALSE;
  2523. if (bfd_bread ((void *) raw_armap, parsed_size, abfd) != parsed_size)
  2524. {
  2525. if (bfd_get_error () != bfd_error_system_call)
  2526. bfd_set_error (bfd_error_malformed_archive);
  2527. bfd_release (abfd, (void *) raw_armap);
  2528. return FALSE;
  2529. }
  2530. ardata->tdata = (void *) raw_armap;
  2531. count = H_GET_32 (abfd, raw_armap);
  2532. ardata->symdef_count = 0;
  2533. ardata->cache = NULL;
  2534. /* This code used to overlay the symdefs over the raw archive data,
  2535. but that doesn't work on a 64 bit host. */
  2536. stringbase = raw_armap + count * 8 + 8;
  2537. #ifdef CHECK_ARMAP_HASH
  2538. {
  2539. unsigned int hlog;
  2540. /* Double check that I have the hashing algorithm right by making
  2541. sure that every symbol can be looked up successfully. */
  2542. hlog = 0;
  2543. for (i = 1; i < count; i <<= 1)
  2544. hlog++;
  2545. BFD_ASSERT (i == count);
  2546. raw_ptr = raw_armap + 4;
  2547. for (i = 0; i < count; i++, raw_ptr += 8)
  2548. {
  2549. unsigned int name_offset, file_offset;
  2550. unsigned int hash, rehash, srch;
  2551. name_offset = H_GET_32 (abfd, raw_ptr);
  2552. file_offset = H_GET_32 (abfd, (raw_ptr + 4));
  2553. if (file_offset == 0)
  2554. continue;
  2555. hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
  2556. hlog);
  2557. if (hash == i)
  2558. continue;
  2559. /* See if we can rehash to this location. */
  2560. for (srch = (hash + rehash) & (count - 1);
  2561. srch != hash && srch != i;
  2562. srch = (srch + rehash) & (count - 1))
  2563. BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
  2564. BFD_ASSERT (srch == i);
  2565. }
  2566. }
  2567. #endif /* CHECK_ARMAP_HASH */
  2568. raw_ptr = raw_armap + 4;
  2569. for (i = 0; i < count; i++, raw_ptr += 8)
  2570. if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
  2571. ++ardata->symdef_count;
  2572. amt = ardata->symdef_count;
  2573. amt *= sizeof (carsym);
  2574. symdef_ptr = (carsym *) bfd_alloc (abfd, amt);
  2575. if (!symdef_ptr)
  2576. return FALSE;
  2577. ardata->symdefs = symdef_ptr;
  2578. raw_ptr = raw_armap + 4;
  2579. for (i = 0; i < count; i++, raw_ptr += 8)
  2580. {
  2581. unsigned int name_offset, file_offset;
  2582. file_offset = H_GET_32 (abfd, (raw_ptr + 4));
  2583. if (file_offset == 0)
  2584. continue;
  2585. name_offset = H_GET_32 (abfd, raw_ptr);
  2586. symdef_ptr->name = stringbase + name_offset;
  2587. symdef_ptr->file_offset = file_offset;
  2588. ++symdef_ptr;
  2589. }
  2590. ardata->first_file_filepos = bfd_tell (abfd);
  2591. /* Pad to an even boundary. */
  2592. ardata->first_file_filepos += ardata->first_file_filepos % 2;
  2593. bfd_has_map (abfd) = TRUE;
  2594. return TRUE;
  2595. }
  2596. /* Write out an armap. */
  2597. bfd_boolean
  2598. _bfd_ecoff_write_armap (bfd *abfd,
  2599. unsigned int elength,
  2600. struct orl *map,
  2601. unsigned int orl_count,
  2602. int stridx)
  2603. {
  2604. unsigned int hashsize, hashlog;
  2605. bfd_size_type symdefsize;
  2606. int padit;
  2607. unsigned int stringsize;
  2608. unsigned int mapsize;
  2609. file_ptr firstreal;
  2610. struct ar_hdr hdr;
  2611. struct stat statbuf;
  2612. unsigned int i;
  2613. bfd_byte temp[4];
  2614. bfd_byte *hashtable;
  2615. bfd *current;
  2616. bfd *last_elt;
  2617. /* Ultrix appears to use as a hash table size the least power of two
  2618. greater than twice the number of entries. */
  2619. for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
  2620. ;
  2621. hashsize = 1 << hashlog;
  2622. symdefsize = hashsize * 8;
  2623. padit = stridx % 2;
  2624. stringsize = stridx + padit;
  2625. /* Include 8 bytes to store symdefsize and stringsize in output. */
  2626. mapsize = symdefsize + stringsize + 8;
  2627. firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
  2628. memset ((void *) &hdr, 0, sizeof hdr);
  2629. /* Work out the ECOFF armap name. */
  2630. strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
  2631. hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
  2632. hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
  2633. (bfd_header_big_endian (abfd)
  2634. ? ARMAP_BIG_ENDIAN
  2635. : ARMAP_LITTLE_ENDIAN);
  2636. hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
  2637. hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
  2638. bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
  2639. memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
  2640. /* Write the timestamp of the archive header to be just a little bit
  2641. later than the timestamp of the file, otherwise the linker will
  2642. complain that the index is out of date. Actually, the Ultrix
  2643. linker just checks the archive name; the GNU linker may check the
  2644. date. */
  2645. stat (abfd->filename, &statbuf);
  2646. _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
  2647. (long) (statbuf.st_mtime + 60));
  2648. /* The DECstation uses zeroes for the uid, gid and mode of the
  2649. armap. */
  2650. hdr.ar_uid[0] = '0';
  2651. hdr.ar_gid[0] = '0';
  2652. /* Building gcc ends up extracting the armap as a file - twice. */
  2653. hdr.ar_mode[0] = '6';
  2654. hdr.ar_mode[1] = '4';
  2655. hdr.ar_mode[2] = '4';
  2656. _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
  2657. hdr.ar_fmag[0] = '`';
  2658. hdr.ar_fmag[1] = '\012';
  2659. /* Turn all null bytes in the header into spaces. */
  2660. for (i = 0; i < sizeof (struct ar_hdr); i++)
  2661. if (((char *) (&hdr))[i] == '\0')
  2662. (((char *) (&hdr))[i]) = ' ';
  2663. if (bfd_bwrite ((void *) &hdr, (bfd_size_type) sizeof (struct ar_hdr), abfd)
  2664. != sizeof (struct ar_hdr))
  2665. return FALSE;
  2666. H_PUT_32 (abfd, hashsize, temp);
  2667. if (bfd_bwrite ((void *) temp, (bfd_size_type) 4, abfd) != 4)
  2668. return FALSE;
  2669. hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
  2670. if (!hashtable)
  2671. return FALSE;
  2672. current = abfd->archive_head;
  2673. last_elt = current;
  2674. for (i = 0; i < orl_count; i++)
  2675. {
  2676. unsigned int hash, rehash = 0;
  2677. /* Advance firstreal to the file position of this archive
  2678. element. */
  2679. if (map[i].u.abfd != last_elt)
  2680. {
  2681. do
  2682. {
  2683. firstreal += arelt_size (current) + sizeof (struct ar_hdr);
  2684. firstreal += firstreal % 2;
  2685. current = current->archive_next;
  2686. }
  2687. while (current != map[i].u.abfd);
  2688. }
  2689. last_elt = current;
  2690. hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
  2691. if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
  2692. {
  2693. unsigned int srch;
  2694. /* The desired slot is already taken. */
  2695. for (srch = (hash + rehash) & (hashsize - 1);
  2696. srch != hash;
  2697. srch = (srch + rehash) & (hashsize - 1))
  2698. if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
  2699. break;
  2700. BFD_ASSERT (srch != hash);
  2701. hash = srch;
  2702. }
  2703. H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8));
  2704. H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4));
  2705. }
  2706. if (bfd_bwrite ((void *) hashtable, symdefsize, abfd) != symdefsize)
  2707. return FALSE;
  2708. bfd_release (abfd, hashtable);
  2709. /* Now write the strings. */
  2710. H_PUT_32 (abfd, stringsize, temp);
  2711. if (bfd_bwrite ((void *) temp, (bfd_size_type) 4, abfd) != 4)
  2712. return FALSE;
  2713. for (i = 0; i < orl_count; i++)
  2714. {
  2715. bfd_size_type len;
  2716. len = strlen (*map[i].name) + 1;
  2717. if (bfd_bwrite ((void *) (*map[i].name), len, abfd) != len)
  2718. return FALSE;
  2719. }
  2720. /* The spec sez this should be a newline. But in order to be
  2721. bug-compatible for DECstation ar we use a null. */
  2722. if (padit)
  2723. {
  2724. if (bfd_bwrite ("", (bfd_size_type) 1, abfd) != 1)
  2725. return FALSE;
  2726. }
  2727. return TRUE;
  2728. }
  2729. /* ECOFF linker code. */
  2730. /* Routine to create an entry in an ECOFF link hash table. */
  2731. static struct bfd_hash_entry *
  2732. ecoff_link_hash_newfunc (struct bfd_hash_entry *entry,
  2733. struct bfd_hash_table *table,
  2734. const char *string)
  2735. {
  2736. struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
  2737. /* Allocate the structure if it has not already been allocated by a
  2738. subclass. */
  2739. if (ret == NULL)
  2740. ret = ((struct ecoff_link_hash_entry *)
  2741. bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
  2742. if (ret == NULL)
  2743. return NULL;
  2744. /* Call the allocation method of the superclass. */
  2745. ret = ((struct ecoff_link_hash_entry *)
  2746. _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  2747. table, string));
  2748. if (ret)
  2749. {
  2750. /* Set local fields. */
  2751. ret->indx = -1;
  2752. ret->abfd = NULL;
  2753. ret->written = 0;
  2754. ret->small = 0;
  2755. }
  2756. memset ((void *) &ret->esym, 0, sizeof ret->esym);
  2757. return (struct bfd_hash_entry *) ret;
  2758. }
  2759. /* Create an ECOFF link hash table. */
  2760. struct bfd_link_hash_table *
  2761. _bfd_ecoff_bfd_link_hash_table_create (bfd *abfd)
  2762. {
  2763. struct ecoff_link_hash_table *ret;
  2764. bfd_size_type amt = sizeof (struct ecoff_link_hash_table);
  2765. ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
  2766. if (ret == NULL)
  2767. return NULL;
  2768. if (!_bfd_link_hash_table_init (&ret->root, abfd,
  2769. ecoff_link_hash_newfunc,
  2770. sizeof (struct ecoff_link_hash_entry)))
  2771. {
  2772. free (ret);
  2773. return NULL;
  2774. }
  2775. return &ret->root;
  2776. }
  2777. /* Look up an entry in an ECOFF link hash table. */
  2778. #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
  2779. ((struct ecoff_link_hash_entry *) \
  2780. bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
  2781. /* Get the ECOFF link hash table from the info structure. This is
  2782. just a cast. */
  2783. #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
  2784. /* Add the external symbols of an object file to the global linker
  2785. hash table. The external symbols and strings we are passed are
  2786. just allocated on the stack, and will be discarded. We must
  2787. explicitly save any information we may need later on in the link.
  2788. We do not want to read the external symbol information again. */
  2789. static bfd_boolean
  2790. ecoff_link_add_externals (bfd *abfd,
  2791. struct bfd_link_info *info,
  2792. void * external_ext,
  2793. char *ssext)
  2794. {
  2795. const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
  2796. void (* const swap_ext_in) (bfd *, void *, EXTR *)
  2797. = backend->debug_swap.swap_ext_in;
  2798. bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
  2799. unsigned long ext_count;
  2800. struct bfd_link_hash_entry **sym_hash;
  2801. char *ext_ptr;
  2802. char *ext_end;
  2803. bfd_size_type amt;
  2804. ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
  2805. amt = ext_count;
  2806. amt *= sizeof (struct bfd_link_hash_entry *);
  2807. sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt);
  2808. if (!sym_hash)
  2809. return FALSE;
  2810. ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash;
  2811. ext_ptr = (char *) external_ext;
  2812. ext_end = ext_ptr + ext_count * external_ext_size;
  2813. for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
  2814. {
  2815. EXTR esym;
  2816. bfd_boolean skip;
  2817. bfd_vma value;
  2818. asection *section;
  2819. const char *name;
  2820. struct ecoff_link_hash_entry *h;
  2821. *sym_hash = NULL;
  2822. (*swap_ext_in) (abfd, (void *) ext_ptr, &esym);
  2823. /* Skip debugging symbols. */
  2824. skip = FALSE;
  2825. switch (esym.asym.st)
  2826. {
  2827. case stGlobal:
  2828. case stStatic:
  2829. case stLabel:
  2830. case stProc:
  2831. case stStaticProc:
  2832. break;
  2833. default:
  2834. skip = TRUE;
  2835. break;
  2836. }
  2837. if (skip)
  2838. continue;
  2839. /* Get the information for this symbol. */
  2840. value = esym.asym.value;
  2841. switch (esym.asym.sc)
  2842. {
  2843. default:
  2844. case scNil:
  2845. case scRegister:
  2846. case scCdbLocal:
  2847. case scBits:
  2848. case scCdbSystem:
  2849. case scRegImage:
  2850. case scInfo:
  2851. case scUserStruct:
  2852. case scVar:
  2853. case scVarRegister:
  2854. case scVariant:
  2855. case scBasedVar:
  2856. case scXData:
  2857. case scPData:
  2858. section = NULL;
  2859. break;
  2860. case scText:
  2861. section = bfd_make_section_old_way (abfd, _TEXT);
  2862. value -= section->vma;
  2863. break;
  2864. case scData:
  2865. section = bfd_make_section_old_way (abfd, _DATA);
  2866. value -= section->vma;
  2867. break;
  2868. case scBss:
  2869. section = bfd_make_section_old_way (abfd, _BSS);
  2870. value -= section->vma;
  2871. break;
  2872. case scAbs:
  2873. section = bfd_abs_section_ptr;
  2874. break;
  2875. case scUndefined:
  2876. section = bfd_und_section_ptr;
  2877. break;
  2878. case scSData:
  2879. section = bfd_make_section_old_way (abfd, _SDATA);
  2880. value -= section->vma;
  2881. break;
  2882. case scSBss:
  2883. section = bfd_make_section_old_way (abfd, _SBSS);
  2884. value -= section->vma;
  2885. break;
  2886. case scRData:
  2887. section = bfd_make_section_old_way (abfd, _RDATA);
  2888. value -= section->vma;
  2889. break;
  2890. case scCommon:
  2891. if (value > ecoff_data (abfd)->gp_size)
  2892. {
  2893. section = bfd_com_section_ptr;
  2894. break;
  2895. }
  2896. /* Fall through. */
  2897. case scSCommon:
  2898. if (ecoff_scom_section.name == NULL)
  2899. {
  2900. /* Initialize the small common section. */
  2901. ecoff_scom_section.name = SCOMMON;
  2902. ecoff_scom_section.flags = SEC_IS_COMMON;
  2903. ecoff_scom_section.output_section = &ecoff_scom_section;
  2904. ecoff_scom_section.symbol = &ecoff_scom_symbol;
  2905. ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
  2906. ecoff_scom_symbol.name = SCOMMON;
  2907. ecoff_scom_symbol.flags = BSF_SECTION_SYM;
  2908. ecoff_scom_symbol.section = &ecoff_scom_section;
  2909. ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
  2910. }
  2911. section = &ecoff_scom_section;
  2912. break;
  2913. case scSUndefined:
  2914. section = bfd_und_section_ptr;
  2915. break;
  2916. case scInit:
  2917. section = bfd_make_section_old_way (abfd, _INIT);
  2918. value -= section->vma;
  2919. break;
  2920. case scFini:
  2921. section = bfd_make_section_old_way (abfd, _FINI);
  2922. value -= section->vma;
  2923. break;
  2924. case scRConst:
  2925. section = bfd_make_section_old_way (abfd, _RCONST);
  2926. value -= section->vma;
  2927. break;
  2928. }
  2929. if (section == NULL)
  2930. continue;
  2931. name = ssext + esym.asym.iss;
  2932. if (! (_bfd_generic_link_add_one_symbol
  2933. (info, abfd, name,
  2934. (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
  2935. section, value, NULL, TRUE, TRUE, sym_hash)))
  2936. return FALSE;
  2937. h = (struct ecoff_link_hash_entry *) *sym_hash;
  2938. /* If we are building an ECOFF hash table, save the external
  2939. symbol information. */
  2940. if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
  2941. {
  2942. if (h->abfd == NULL
  2943. || (! bfd_is_und_section (section)
  2944. && (! bfd_is_com_section (section)
  2945. || (h->root.type != bfd_link_hash_defined
  2946. && h->root.type != bfd_link_hash_defweak))))
  2947. {
  2948. h->abfd = abfd;
  2949. h->esym = esym;
  2950. }
  2951. /* Remember whether this symbol was small undefined. */
  2952. if (esym.asym.sc == scSUndefined)
  2953. h->small = 1;
  2954. /* If this symbol was ever small undefined, it needs to wind
  2955. up in a GP relative section. We can't control the
  2956. section of a defined symbol, but we can control the
  2957. section of a common symbol. This case is actually needed
  2958. on Ultrix 4.2 to handle the symbol cred in -lckrb. */
  2959. if (h->small
  2960. && h->root.type == bfd_link_hash_common
  2961. && streq (h->root.u.c.p->section->name, SCOMMON))
  2962. {
  2963. h->root.u.c.p->section = bfd_make_section_old_way (abfd,
  2964. SCOMMON);
  2965. h->root.u.c.p->section->flags = SEC_ALLOC;
  2966. if (h->esym.asym.sc == scCommon)
  2967. h->esym.asym.sc = scSCommon;
  2968. }
  2969. }
  2970. }
  2971. return TRUE;
  2972. }
  2973. /* Add symbols from an ECOFF object file to the global linker hash
  2974. table. */
  2975. static bfd_boolean
  2976. ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
  2977. {
  2978. HDRR *symhdr;
  2979. bfd_size_type external_ext_size;
  2980. void * external_ext = NULL;
  2981. bfd_size_type esize;
  2982. char *ssext = NULL;
  2983. bfd_boolean result;
  2984. if (! ecoff_slurp_symbolic_header (abfd))
  2985. return FALSE;
  2986. /* If there are no symbols, we don't want it. */
  2987. if (bfd_get_symcount (abfd) == 0)
  2988. return TRUE;
  2989. symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
  2990. /* Read in the external symbols and external strings. */
  2991. external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
  2992. esize = symhdr->iextMax * external_ext_size;
  2993. external_ext = bfd_malloc (esize);
  2994. if (external_ext == NULL && esize != 0)
  2995. goto error_return;
  2996. if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
  2997. || bfd_bread (external_ext, esize, abfd) != esize)
  2998. goto error_return;
  2999. ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
  3000. if (ssext == NULL && symhdr->issExtMax != 0)
  3001. goto error_return;
  3002. if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
  3003. || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
  3004. != (bfd_size_type) symhdr->issExtMax))
  3005. goto error_return;
  3006. result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
  3007. if (ssext != NULL)
  3008. free (ssext);
  3009. if (external_ext != NULL)
  3010. free (external_ext);
  3011. return result;
  3012. error_return:
  3013. if (ssext != NULL)
  3014. free (ssext);
  3015. if (external_ext != NULL)
  3016. free (external_ext);
  3017. return FALSE;
  3018. }
  3019. /* This is called if we used _bfd_generic_link_add_archive_symbols
  3020. because we were not dealing with an ECOFF archive. */
  3021. static bfd_boolean
  3022. ecoff_link_check_archive_element (bfd *abfd,
  3023. struct bfd_link_info *info,
  3024. struct bfd_link_hash_entry *h,
  3025. const char *name,
  3026. bfd_boolean *pneeded)
  3027. {
  3028. *pneeded = FALSE;
  3029. /* Unlike the generic linker, we do not pull in elements because
  3030. of common symbols. */
  3031. if (h->type != bfd_link_hash_undefined)
  3032. return TRUE;
  3033. /* Include this element. */
  3034. if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
  3035. return FALSE;
  3036. *pneeded = TRUE;
  3037. return ecoff_link_add_object_symbols (abfd, info);
  3038. }
  3039. /* Add the symbols from an archive file to the global hash table.
  3040. This looks through the undefined symbols, looks each one up in the
  3041. archive hash table, and adds any associated object file. We do not
  3042. use _bfd_generic_link_add_archive_symbols because ECOFF archives
  3043. already have a hash table, so there is no reason to construct
  3044. another one. */
  3045. static bfd_boolean
  3046. ecoff_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
  3047. {
  3048. const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
  3049. const bfd_byte *raw_armap;
  3050. struct bfd_link_hash_entry **pundef;
  3051. unsigned int armap_count;
  3052. unsigned int armap_log;
  3053. unsigned int i;
  3054. const bfd_byte *hashtable;
  3055. const char *stringbase;
  3056. if (! bfd_has_map (abfd))
  3057. {
  3058. /* An empty archive is a special case. */
  3059. if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
  3060. return TRUE;
  3061. bfd_set_error (bfd_error_no_armap);
  3062. return FALSE;
  3063. }
  3064. /* If we don't have any raw data for this archive, as can happen on
  3065. Irix 4.0.5F, we call the generic routine.
  3066. FIXME: We should be more clever about this, since someday tdata
  3067. may get to something for a generic archive. */
  3068. raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
  3069. if (raw_armap == NULL)
  3070. return (_bfd_generic_link_add_archive_symbols
  3071. (abfd, info, ecoff_link_check_archive_element));
  3072. armap_count = H_GET_32 (abfd, raw_armap);
  3073. armap_log = 0;
  3074. for (i = 1; i < armap_count; i <<= 1)
  3075. armap_log++;
  3076. BFD_ASSERT (i == armap_count);
  3077. hashtable = raw_armap + 4;
  3078. stringbase = (const char *) raw_armap + armap_count * 8 + 8;
  3079. /* Look through the list of undefined symbols. */
  3080. pundef = &info->hash->undefs;
  3081. while (*pundef != NULL)
  3082. {
  3083. struct bfd_link_hash_entry *h;
  3084. unsigned int hash, rehash = 0;
  3085. unsigned int file_offset;
  3086. const char *name;
  3087. bfd *element;
  3088. h = *pundef;
  3089. /* When a symbol is defined, it is not necessarily removed from
  3090. the list. */
  3091. if (h->type != bfd_link_hash_undefined
  3092. && h->type != bfd_link_hash_common)
  3093. {
  3094. /* Remove this entry from the list, for general cleanliness
  3095. and because we are going to look through the list again
  3096. if we search any more libraries. We can't remove the
  3097. entry if it is the tail, because that would lose any
  3098. entries we add to the list later on. */
  3099. if (*pundef != info->hash->undefs_tail)
  3100. *pundef = (*pundef)->u.undef.next;
  3101. else
  3102. pundef = &(*pundef)->u.undef.next;
  3103. continue;
  3104. }
  3105. /* Native ECOFF linkers do not pull in archive elements merely
  3106. to satisfy common definitions, so neither do we. We leave
  3107. them on the list, though, in case we are linking against some
  3108. other object format. */
  3109. if (h->type != bfd_link_hash_undefined)
  3110. {
  3111. pundef = &(*pundef)->u.undef.next;
  3112. continue;
  3113. }
  3114. /* Look for this symbol in the archive hash table. */
  3115. hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
  3116. armap_log);
  3117. file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
  3118. if (file_offset == 0)
  3119. {
  3120. /* Nothing in this slot. */
  3121. pundef = &(*pundef)->u.undef.next;
  3122. continue;
  3123. }
  3124. name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
  3125. if (name[0] != h->root.string[0]
  3126. || ! streq (name, h->root.string))
  3127. {
  3128. unsigned int srch;
  3129. bfd_boolean found;
  3130. /* That was the wrong symbol. Try rehashing. */
  3131. found = FALSE;
  3132. for (srch = (hash + rehash) & (armap_count - 1);
  3133. srch != hash;
  3134. srch = (srch + rehash) & (armap_count - 1))
  3135. {
  3136. file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
  3137. if (file_offset == 0)
  3138. break;
  3139. name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
  3140. if (name[0] == h->root.string[0]
  3141. && streq (name, h->root.string))
  3142. {
  3143. found = TRUE;
  3144. break;
  3145. }
  3146. }
  3147. if (! found)
  3148. {
  3149. pundef = &(*pundef)->u.undef.next;
  3150. continue;
  3151. }
  3152. hash = srch;
  3153. }
  3154. element = (*backend->get_elt_at_filepos) (abfd, (file_ptr) file_offset);
  3155. if (element == NULL)
  3156. return FALSE;
  3157. if (! bfd_check_format (element, bfd_object))
  3158. return FALSE;
  3159. /* Unlike the generic linker, we know that this element provides
  3160. a definition for an undefined symbol and we know that we want
  3161. to include it. We don't need to check anything. */
  3162. if (!(*info->callbacks
  3163. ->add_archive_element) (info, element, name, &element))
  3164. return FALSE;
  3165. if (! ecoff_link_add_object_symbols (element, info))
  3166. return FALSE;
  3167. pundef = &(*pundef)->u.undef.next;
  3168. }
  3169. return TRUE;
  3170. }
  3171. /* Given an ECOFF BFD, add symbols to the global hash table as
  3172. appropriate. */
  3173. bfd_boolean
  3174. _bfd_ecoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  3175. {
  3176. switch (bfd_get_format (abfd))
  3177. {
  3178. case bfd_object:
  3179. return ecoff_link_add_object_symbols (abfd, info);
  3180. case bfd_archive:
  3181. return ecoff_link_add_archive_symbols (abfd, info);
  3182. default:
  3183. bfd_set_error (bfd_error_wrong_format);
  3184. return FALSE;
  3185. }
  3186. }
  3187. /* ECOFF final link routines. */
  3188. /* Structure used to pass information to ecoff_link_write_external. */
  3189. struct extsym_info
  3190. {
  3191. bfd *abfd;
  3192. struct bfd_link_info *info;
  3193. };
  3194. /* Accumulate the debugging information for an input BFD into the
  3195. output BFD. This must read in the symbolic information of the
  3196. input BFD. */
  3197. static bfd_boolean
  3198. ecoff_final_link_debug_accumulate (bfd *output_bfd,
  3199. bfd *input_bfd,
  3200. struct bfd_link_info *info,
  3201. void * handle)
  3202. {
  3203. struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
  3204. const struct ecoff_debug_swap * const swap =
  3205. &ecoff_backend (input_bfd)->debug_swap;
  3206. HDRR *symhdr = &debug->symbolic_header;
  3207. bfd_boolean ret;
  3208. #define READ(ptr, offset, count, size, type) \
  3209. if (symhdr->count == 0) \
  3210. debug->ptr = NULL; \
  3211. else \
  3212. { \
  3213. bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
  3214. debug->ptr = (type) bfd_malloc (amt); \
  3215. if (debug->ptr == NULL) \
  3216. { \
  3217. ret = FALSE; \
  3218. goto return_something; \
  3219. } \
  3220. if (bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
  3221. || bfd_bread (debug->ptr, amt, input_bfd) != amt) \
  3222. { \
  3223. ret = FALSE; \
  3224. goto return_something; \
  3225. } \
  3226. }
  3227. /* If raw_syments is not NULL, then the data was already by read by
  3228. _bfd_ecoff_slurp_symbolic_info. */
  3229. if (ecoff_data (input_bfd)->raw_syments == NULL)
  3230. {
  3231. READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
  3232. unsigned char *);
  3233. READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
  3234. READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
  3235. READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
  3236. READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
  3237. READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
  3238. union aux_ext *);
  3239. READ (ss, cbSsOffset, issMax, sizeof (char), char *);
  3240. READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
  3241. READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
  3242. }
  3243. #undef READ
  3244. /* We do not read the external strings or the external symbols. */
  3245. ret = (bfd_ecoff_debug_accumulate
  3246. (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
  3247. &ecoff_backend (output_bfd)->debug_swap,
  3248. input_bfd, debug, swap, info));
  3249. return_something:
  3250. if (ecoff_data (input_bfd)->raw_syments == NULL)
  3251. {
  3252. if (debug->line != NULL)
  3253. free (debug->line);
  3254. if (debug->external_dnr != NULL)
  3255. free (debug->external_dnr);
  3256. if (debug->external_pdr != NULL)
  3257. free (debug->external_pdr);
  3258. if (debug->external_sym != NULL)
  3259. free (debug->external_sym);
  3260. if (debug->external_opt != NULL)
  3261. free (debug->external_opt);
  3262. if (debug->external_aux != NULL)
  3263. free (debug->external_aux);
  3264. if (debug->ss != NULL)
  3265. free (debug->ss);
  3266. if (debug->external_fdr != NULL)
  3267. free (debug->external_fdr);
  3268. if (debug->external_rfd != NULL)
  3269. free (debug->external_rfd);
  3270. /* Make sure we don't accidentally follow one of these pointers
  3271. into freed memory. */
  3272. debug->line = NULL;
  3273. debug->external_dnr = NULL;
  3274. debug->external_pdr = NULL;
  3275. debug->external_sym = NULL;
  3276. debug->external_opt = NULL;
  3277. debug->external_aux = NULL;
  3278. debug->ss = NULL;
  3279. debug->external_fdr = NULL;
  3280. debug->external_rfd = NULL;
  3281. }
  3282. return ret;
  3283. }
  3284. /* Relocate and write an ECOFF section into an ECOFF output file. */
  3285. static bfd_boolean
  3286. ecoff_indirect_link_order (bfd *output_bfd,
  3287. struct bfd_link_info *info,
  3288. asection *output_section,
  3289. struct bfd_link_order *link_order)
  3290. {
  3291. asection *input_section;
  3292. bfd *input_bfd;
  3293. bfd_byte *contents = NULL;
  3294. bfd_size_type external_reloc_size;
  3295. bfd_size_type external_relocs_size;
  3296. void * external_relocs = NULL;
  3297. BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
  3298. input_section = link_order->u.indirect.section;
  3299. input_bfd = input_section->owner;
  3300. if (input_section->size == 0)
  3301. return TRUE;
  3302. BFD_ASSERT (input_section->output_section == output_section);
  3303. BFD_ASSERT (input_section->output_offset == link_order->offset);
  3304. BFD_ASSERT (input_section->size == link_order->size);
  3305. /* Get the section contents. */
  3306. if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
  3307. goto error_return;
  3308. /* Get the relocs. If we are relaxing MIPS code, they will already
  3309. have been read in. Otherwise, we read them in now. */
  3310. external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
  3311. external_relocs_size = external_reloc_size * input_section->reloc_count;
  3312. external_relocs = bfd_malloc (external_relocs_size);
  3313. if (external_relocs == NULL && external_relocs_size != 0)
  3314. goto error_return;
  3315. if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
  3316. || (bfd_bread (external_relocs, external_relocs_size, input_bfd)
  3317. != external_relocs_size))
  3318. goto error_return;
  3319. /* Relocate the section contents. */
  3320. if (! ((*ecoff_backend (input_bfd)->relocate_section)
  3321. (output_bfd, info, input_bfd, input_section, contents,
  3322. external_relocs)))
  3323. goto error_return;
  3324. /* Write out the relocated section. */
  3325. if (! bfd_set_section_contents (output_bfd,
  3326. output_section,
  3327. contents,
  3328. input_section->output_offset,
  3329. input_section->size))
  3330. goto error_return;
  3331. /* If we are producing relocatable output, the relocs were
  3332. modified, and we write them out now. We use the reloc_count
  3333. field of output_section to keep track of the number of relocs we
  3334. have output so far. */
  3335. if (bfd_link_relocatable (info))
  3336. {
  3337. file_ptr pos = (output_section->rel_filepos
  3338. + output_section->reloc_count * external_reloc_size);
  3339. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  3340. || (bfd_bwrite (external_relocs, external_relocs_size, output_bfd)
  3341. != external_relocs_size))
  3342. goto error_return;
  3343. output_section->reloc_count += input_section->reloc_count;
  3344. }
  3345. if (contents != NULL)
  3346. free (contents);
  3347. if (external_relocs != NULL)
  3348. free (external_relocs);
  3349. return TRUE;
  3350. error_return:
  3351. if (contents != NULL)
  3352. free (contents);
  3353. if (external_relocs != NULL)
  3354. free (external_relocs);
  3355. return FALSE;
  3356. }
  3357. /* Generate a reloc when linking an ECOFF file. This is a reloc
  3358. requested by the linker, and does come from any input file. This
  3359. is used to build constructor and destructor tables when linking
  3360. with -Ur. */
  3361. static bfd_boolean
  3362. ecoff_reloc_link_order (bfd *output_bfd,
  3363. struct bfd_link_info *info,
  3364. asection *output_section,
  3365. struct bfd_link_order *link_order)
  3366. {
  3367. enum bfd_link_order_type type;
  3368. asection *section;
  3369. bfd_vma addend;
  3370. arelent rel;
  3371. struct internal_reloc in;
  3372. bfd_size_type external_reloc_size;
  3373. bfd_byte *rbuf;
  3374. bfd_boolean ok;
  3375. file_ptr pos;
  3376. type = link_order->type;
  3377. section = NULL;
  3378. addend = link_order->u.reloc.p->addend;
  3379. /* We set up an arelent to pass to the backend adjust_reloc_out
  3380. routine. */
  3381. rel.address = link_order->offset;
  3382. rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
  3383. if (rel.howto == 0)
  3384. {
  3385. bfd_set_error (bfd_error_bad_value);
  3386. return FALSE;
  3387. }
  3388. if (type == bfd_section_reloc_link_order)
  3389. {
  3390. section = link_order->u.reloc.p->u.section;
  3391. rel.sym_ptr_ptr = section->symbol_ptr_ptr;
  3392. }
  3393. else
  3394. {
  3395. struct bfd_link_hash_entry *h;
  3396. /* Treat a reloc against a defined symbol as though it were
  3397. actually against the section. */
  3398. h = bfd_wrapped_link_hash_lookup (output_bfd, info,
  3399. link_order->u.reloc.p->u.name,
  3400. FALSE, FALSE, FALSE);
  3401. if (h != NULL
  3402. && (h->type == bfd_link_hash_defined
  3403. || h->type == bfd_link_hash_defweak))
  3404. {
  3405. type = bfd_section_reloc_link_order;
  3406. section = h->u.def.section->output_section;
  3407. /* It seems that we ought to add the symbol value to the
  3408. addend here, but in practice it has already been added
  3409. because it was passed to constructor_callback. */
  3410. addend += section->vma + h->u.def.section->output_offset;
  3411. }
  3412. else
  3413. {
  3414. /* We can't set up a reloc against a symbol correctly,
  3415. because we have no asymbol structure. Currently no
  3416. adjust_reloc_out routine cares. */
  3417. rel.sym_ptr_ptr = NULL;
  3418. }
  3419. }
  3420. /* All ECOFF relocs are in-place. Put the addend into the object
  3421. file. */
  3422. BFD_ASSERT (rel.howto->partial_inplace);
  3423. if (addend != 0)
  3424. {
  3425. bfd_size_type size;
  3426. bfd_reloc_status_type rstat;
  3427. bfd_byte *buf;
  3428. size = bfd_get_reloc_size (rel.howto);
  3429. buf = (bfd_byte *) bfd_zmalloc (size);
  3430. if (buf == NULL && size != 0)
  3431. return FALSE;
  3432. rstat = _bfd_relocate_contents (rel.howto, output_bfd,
  3433. (bfd_vma) addend, buf);
  3434. switch (rstat)
  3435. {
  3436. case bfd_reloc_ok:
  3437. break;
  3438. default:
  3439. case bfd_reloc_outofrange:
  3440. abort ();
  3441. case bfd_reloc_overflow:
  3442. if (! ((*info->callbacks->reloc_overflow)
  3443. (info, NULL,
  3444. (link_order->type == bfd_section_reloc_link_order
  3445. ? bfd_section_name (output_bfd, section)
  3446. : link_order->u.reloc.p->u.name),
  3447. rel.howto->name, addend, NULL,
  3448. NULL, (bfd_vma) 0)))
  3449. {
  3450. free (buf);
  3451. return FALSE;
  3452. }
  3453. break;
  3454. }
  3455. ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
  3456. (file_ptr) link_order->offset, size);
  3457. free (buf);
  3458. if (! ok)
  3459. return FALSE;
  3460. }
  3461. rel.addend = 0;
  3462. /* Move the information into an internal_reloc structure. */
  3463. in.r_vaddr = (rel.address
  3464. + bfd_get_section_vma (output_bfd, output_section));
  3465. in.r_type = rel.howto->type;
  3466. if (type == bfd_symbol_reloc_link_order)
  3467. {
  3468. struct ecoff_link_hash_entry *h;
  3469. h = ((struct ecoff_link_hash_entry *)
  3470. bfd_wrapped_link_hash_lookup (output_bfd, info,
  3471. link_order->u.reloc.p->u.name,
  3472. FALSE, FALSE, TRUE));
  3473. if (h != NULL
  3474. && h->indx != -1)
  3475. in.r_symndx = h->indx;
  3476. else
  3477. {
  3478. if (! ((*info->callbacks->unattached_reloc)
  3479. (info, link_order->u.reloc.p->u.name, NULL,
  3480. NULL, (bfd_vma) 0)))
  3481. return FALSE;
  3482. in.r_symndx = 0;
  3483. }
  3484. in.r_extern = 1;
  3485. }
  3486. else
  3487. {
  3488. const char *name;
  3489. unsigned int i;
  3490. static struct
  3491. {
  3492. const char * name;
  3493. long r_symndx;
  3494. }
  3495. section_symndx [] =
  3496. {
  3497. { _TEXT, RELOC_SECTION_TEXT },
  3498. { _RDATA, RELOC_SECTION_RDATA },
  3499. { _DATA, RELOC_SECTION_DATA },
  3500. { _SDATA, RELOC_SECTION_SDATA },
  3501. { _SBSS, RELOC_SECTION_SBSS },
  3502. { _BSS, RELOC_SECTION_BSS },
  3503. { _INIT, RELOC_SECTION_INIT },
  3504. { _LIT8, RELOC_SECTION_LIT8 },
  3505. { _LIT4, RELOC_SECTION_LIT4 },
  3506. { _XDATA, RELOC_SECTION_XDATA },
  3507. { _PDATA, RELOC_SECTION_PDATA },
  3508. { _FINI, RELOC_SECTION_FINI },
  3509. { _LITA, RELOC_SECTION_LITA },
  3510. { "*ABS*", RELOC_SECTION_ABS },
  3511. { _RCONST, RELOC_SECTION_RCONST }
  3512. };
  3513. name = bfd_get_section_name (output_bfd, section);
  3514. for (i = 0; i < ARRAY_SIZE (section_symndx); i++)
  3515. if (streq (name, section_symndx[i].name))
  3516. {
  3517. in.r_symndx = section_symndx[i].r_symndx;
  3518. break;
  3519. }
  3520. if (i == ARRAY_SIZE (section_symndx))
  3521. abort ();
  3522. in.r_extern = 0;
  3523. }
  3524. /* Let the BFD backend adjust the reloc. */
  3525. (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
  3526. /* Get some memory and swap out the reloc. */
  3527. external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
  3528. rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
  3529. if (rbuf == NULL)
  3530. return FALSE;
  3531. (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf);
  3532. pos = (output_section->rel_filepos
  3533. + output_section->reloc_count * external_reloc_size);
  3534. ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
  3535. && (bfd_bwrite ((void *) rbuf, external_reloc_size, output_bfd)
  3536. == external_reloc_size));
  3537. if (ok)
  3538. ++output_section->reloc_count;
  3539. free (rbuf);
  3540. return ok;
  3541. }
  3542. /* Put out information for an external symbol. These come only from
  3543. the hash table. */
  3544. static bfd_boolean
  3545. ecoff_link_write_external (struct bfd_hash_entry *bh, void * data)
  3546. {
  3547. struct ecoff_link_hash_entry *h = (struct ecoff_link_hash_entry *) bh;
  3548. struct extsym_info *einfo = (struct extsym_info *) data;
  3549. bfd *output_bfd = einfo->abfd;
  3550. bfd_boolean strip;
  3551. if (h->root.type == bfd_link_hash_warning)
  3552. {
  3553. h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
  3554. if (h->root.type == bfd_link_hash_new)
  3555. return TRUE;
  3556. }
  3557. /* We need to check if this symbol is being stripped. */
  3558. if (h->root.type == bfd_link_hash_undefined
  3559. || h->root.type == bfd_link_hash_undefweak)
  3560. strip = FALSE;
  3561. else if (einfo->info->strip == strip_all
  3562. || (einfo->info->strip == strip_some
  3563. && bfd_hash_lookup (einfo->info->keep_hash,
  3564. h->root.root.string,
  3565. FALSE, FALSE) == NULL))
  3566. strip = TRUE;
  3567. else
  3568. strip = FALSE;
  3569. if (strip || h->written)
  3570. return TRUE;
  3571. if (h->abfd == NULL)
  3572. {
  3573. h->esym.jmptbl = 0;
  3574. h->esym.cobol_main = 0;
  3575. h->esym.weakext = 0;
  3576. h->esym.reserved = 0;
  3577. h->esym.ifd = ifdNil;
  3578. h->esym.asym.value = 0;
  3579. h->esym.asym.st = stGlobal;
  3580. if (h->root.type != bfd_link_hash_defined
  3581. && h->root.type != bfd_link_hash_defweak)
  3582. h->esym.asym.sc = scAbs;
  3583. else
  3584. {
  3585. asection *output_section;
  3586. const char *name;
  3587. unsigned int i;
  3588. static struct
  3589. {
  3590. const char * name;
  3591. int sc;
  3592. }
  3593. section_storage_classes [] =
  3594. {
  3595. { _TEXT, scText },
  3596. { _DATA, scData },
  3597. { _SDATA, scSData },
  3598. { _RDATA, scRData },
  3599. { _BSS, scBss },
  3600. { _SBSS, scSBss },
  3601. { _INIT, scInit },
  3602. { _FINI, scFini },
  3603. { _PDATA, scPData },
  3604. { _XDATA, scXData },
  3605. { _RCONST, scRConst }
  3606. };
  3607. output_section = h->root.u.def.section->output_section;
  3608. name = bfd_section_name (output_section->owner, output_section);
  3609. for (i = 0; i < ARRAY_SIZE (section_storage_classes); i++)
  3610. if (streq (name, section_storage_classes[i].name))
  3611. {
  3612. h->esym.asym.sc = section_storage_classes[i].sc;
  3613. break;
  3614. }
  3615. if (i == ARRAY_SIZE (section_storage_classes))
  3616. h->esym.asym.sc = scAbs;
  3617. }
  3618. h->esym.asym.reserved = 0;
  3619. h->esym.asym.index = indexNil;
  3620. }
  3621. else if (h->esym.ifd != -1)
  3622. {
  3623. struct ecoff_debug_info *debug;
  3624. /* Adjust the FDR index for the symbol by that used for the
  3625. input BFD. */
  3626. debug = &ecoff_data (h->abfd)->debug_info;
  3627. BFD_ASSERT (h->esym.ifd >= 0
  3628. && h->esym.ifd < debug->symbolic_header.ifdMax);
  3629. h->esym.ifd = debug->ifdmap[h->esym.ifd];
  3630. }
  3631. switch (h->root.type)
  3632. {
  3633. default:
  3634. case bfd_link_hash_warning:
  3635. case bfd_link_hash_new:
  3636. abort ();
  3637. case bfd_link_hash_undefined:
  3638. case bfd_link_hash_undefweak:
  3639. if (h->esym.asym.sc != scUndefined
  3640. && h->esym.asym.sc != scSUndefined)
  3641. h->esym.asym.sc = scUndefined;
  3642. break;
  3643. case bfd_link_hash_defined:
  3644. case bfd_link_hash_defweak:
  3645. if (h->esym.asym.sc == scUndefined
  3646. || h->esym.asym.sc == scSUndefined)
  3647. h->esym.asym.sc = scAbs;
  3648. else if (h->esym.asym.sc == scCommon)
  3649. h->esym.asym.sc = scBss;
  3650. else if (h->esym.asym.sc == scSCommon)
  3651. h->esym.asym.sc = scSBss;
  3652. h->esym.asym.value = (h->root.u.def.value
  3653. + h->root.u.def.section->output_section->vma
  3654. + h->root.u.def.section->output_offset);
  3655. break;
  3656. case bfd_link_hash_common:
  3657. if (h->esym.asym.sc != scCommon
  3658. && h->esym.asym.sc != scSCommon)
  3659. h->esym.asym.sc = scCommon;
  3660. h->esym.asym.value = h->root.u.c.size;
  3661. break;
  3662. case bfd_link_hash_indirect:
  3663. /* We ignore these symbols, since the indirected symbol is
  3664. already in the hash table. */
  3665. return TRUE;
  3666. }
  3667. /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
  3668. symbol number. */
  3669. h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
  3670. h->written = 1;
  3671. return (bfd_ecoff_debug_one_external
  3672. (output_bfd, &ecoff_data (output_bfd)->debug_info,
  3673. &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
  3674. &h->esym));
  3675. }
  3676. /* ECOFF final link routine. This looks through all the input BFDs
  3677. and gathers together all the debugging information, and then
  3678. processes all the link order information. This may cause it to
  3679. close and reopen some input BFDs; I'll see how bad this is. */
  3680. bfd_boolean
  3681. _bfd_ecoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
  3682. {
  3683. const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
  3684. struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
  3685. HDRR *symhdr;
  3686. void * handle;
  3687. bfd *input_bfd;
  3688. asection *o;
  3689. struct bfd_link_order *p;
  3690. struct extsym_info einfo;
  3691. /* We accumulate the debugging information counts in the symbolic
  3692. header. */
  3693. symhdr = &debug->symbolic_header;
  3694. symhdr->vstamp = 0;
  3695. symhdr->ilineMax = 0;
  3696. symhdr->cbLine = 0;
  3697. symhdr->idnMax = 0;
  3698. symhdr->ipdMax = 0;
  3699. symhdr->isymMax = 0;
  3700. symhdr->ioptMax = 0;
  3701. symhdr->iauxMax = 0;
  3702. symhdr->issMax = 0;
  3703. symhdr->issExtMax = 0;
  3704. symhdr->ifdMax = 0;
  3705. symhdr->crfd = 0;
  3706. symhdr->iextMax = 0;
  3707. /* We accumulate the debugging information itself in the debug_info
  3708. structure. */
  3709. debug->line = NULL;
  3710. debug->external_dnr = NULL;
  3711. debug->external_pdr = NULL;
  3712. debug->external_sym = NULL;
  3713. debug->external_opt = NULL;
  3714. debug->external_aux = NULL;
  3715. debug->ss = NULL;
  3716. debug->ssext = debug->ssext_end = NULL;
  3717. debug->external_fdr = NULL;
  3718. debug->external_rfd = NULL;
  3719. debug->external_ext = debug->external_ext_end = NULL;
  3720. handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
  3721. if (handle == NULL)
  3722. return FALSE;
  3723. /* Accumulate the debugging symbols from each input BFD. */
  3724. for (input_bfd = info->input_bfds;
  3725. input_bfd != NULL;
  3726. input_bfd = input_bfd->link.next)
  3727. {
  3728. bfd_boolean ret;
  3729. if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
  3730. {
  3731. /* Arbitrarily set the symbolic header vstamp to the vstamp
  3732. of the first object file in the link. */
  3733. if (symhdr->vstamp == 0)
  3734. symhdr->vstamp
  3735. = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
  3736. ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
  3737. handle);
  3738. }
  3739. else
  3740. ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
  3741. debug, &backend->debug_swap,
  3742. input_bfd, info);
  3743. if (! ret)
  3744. return FALSE;
  3745. /* Combine the register masks. */
  3746. ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
  3747. ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
  3748. ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
  3749. ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
  3750. ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
  3751. ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
  3752. }
  3753. /* Write out the external symbols. */
  3754. einfo.abfd = abfd;
  3755. einfo.info = info;
  3756. bfd_hash_traverse (&info->hash->table, ecoff_link_write_external, &einfo);
  3757. if (bfd_link_relocatable (info))
  3758. {
  3759. /* We need to make a pass over the link_orders to count up the
  3760. number of relocations we will need to output, so that we know
  3761. how much space they will take up. */
  3762. for (o = abfd->sections; o != NULL; o = o->next)
  3763. {
  3764. o->reloc_count = 0;
  3765. for (p = o->map_head.link_order;
  3766. p != NULL;
  3767. p = p->next)
  3768. if (p->type == bfd_indirect_link_order)
  3769. o->reloc_count += p->u.indirect.section->reloc_count;
  3770. else if (p->type == bfd_section_reloc_link_order
  3771. || p->type == bfd_symbol_reloc_link_order)
  3772. ++o->reloc_count;
  3773. }
  3774. }
  3775. /* Compute the reloc and symbol file positions. */
  3776. ecoff_compute_reloc_file_positions (abfd);
  3777. /* Write out the debugging information. */
  3778. if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
  3779. &backend->debug_swap, info,
  3780. ecoff_data (abfd)->sym_filepos))
  3781. return FALSE;
  3782. bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
  3783. if (bfd_link_relocatable (info))
  3784. {
  3785. /* Now reset the reloc_count field of the sections in the output
  3786. BFD to 0, so that we can use them to keep track of how many
  3787. relocs we have output thus far. */
  3788. for (o = abfd->sections; o != NULL; o = o->next)
  3789. o->reloc_count = 0;
  3790. }
  3791. /* Get a value for the GP register. */
  3792. if (ecoff_data (abfd)->gp == 0)
  3793. {
  3794. struct bfd_link_hash_entry *h;
  3795. h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
  3796. if (h != NULL
  3797. && h->type == bfd_link_hash_defined)
  3798. ecoff_data (abfd)->gp = (h->u.def.value
  3799. + h->u.def.section->output_section->vma
  3800. + h->u.def.section->output_offset);
  3801. else if (bfd_link_relocatable (info))
  3802. {
  3803. bfd_vma lo;
  3804. /* Make up a value. */
  3805. lo = (bfd_vma) -1;
  3806. for (o = abfd->sections; o != NULL; o = o->next)
  3807. {
  3808. if (o->vma < lo
  3809. && (streq (o->name, _SBSS)
  3810. || streq (o->name, _SDATA)
  3811. || streq (o->name, _LIT4)
  3812. || streq (o->name, _LIT8)
  3813. || streq (o->name, _LITA)))
  3814. lo = o->vma;
  3815. }
  3816. ecoff_data (abfd)->gp = lo + 0x8000;
  3817. }
  3818. else
  3819. {
  3820. /* If the relocate_section function needs to do a reloc
  3821. involving the GP value, it should make a reloc_dangerous
  3822. callback to warn that GP is not defined. */
  3823. }
  3824. }
  3825. for (o = abfd->sections; o != NULL; o = o->next)
  3826. {
  3827. for (p = o->map_head.link_order;
  3828. p != NULL;
  3829. p = p->next)
  3830. {
  3831. if (p->type == bfd_indirect_link_order
  3832. && (bfd_get_flavour (p->u.indirect.section->owner)
  3833. == bfd_target_ecoff_flavour))
  3834. {
  3835. if (! ecoff_indirect_link_order (abfd, info, o, p))
  3836. return FALSE;
  3837. }
  3838. else if (p->type == bfd_section_reloc_link_order
  3839. || p->type == bfd_symbol_reloc_link_order)
  3840. {
  3841. if (! ecoff_reloc_link_order (abfd, info, o, p))
  3842. return FALSE;
  3843. }
  3844. else
  3845. {
  3846. if (! _bfd_default_link_order (abfd, info, o, p))
  3847. return FALSE;
  3848. }
  3849. }
  3850. }
  3851. bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
  3852. ecoff_data (abfd)->linker = TRUE;
  3853. return TRUE;
  3854. }