ecoff.c 123 KB

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