ecofflink.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. /* Routines to link ECOFF debugging information.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support, <ian@cygnus.com>.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "objalloc.h"
  22. #include "aout/stab_gnu.h"
  23. #include "coff/internal.h"
  24. #include "coff/sym.h"
  25. #include "coff/symconst.h"
  26. #include "coff/ecoff.h"
  27. #include "libcoff.h"
  28. #include "libecoff.h"
  29. /* Routines to swap auxiliary information in and out. I am assuming
  30. that the auxiliary information format is always going to be target
  31. independent. */
  32. /* Swap in a type information record.
  33. BIGEND says whether AUX symbols are big-endian or little-endian; this
  34. info comes from the file header record (fh-fBigendian). */
  35. void
  36. _bfd_ecoff_swap_tir_in (int bigend, const struct tir_ext *ext_copy,
  37. TIR *intern)
  38. {
  39. struct tir_ext ext[1];
  40. *ext = *ext_copy; /* Make it reasonable to do in-place. */
  41. /* now the fun stuff... */
  42. if (bigend)
  43. {
  44. intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
  45. intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
  46. intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
  47. >> TIR_BITS1_BT_SH_BIG;
  48. intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
  49. >> TIR_BITS_TQ4_SH_BIG;
  50. intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
  51. >> TIR_BITS_TQ5_SH_BIG;
  52. intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
  53. >> TIR_BITS_TQ0_SH_BIG;
  54. intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
  55. >> TIR_BITS_TQ1_SH_BIG;
  56. intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
  57. >> TIR_BITS_TQ2_SH_BIG;
  58. intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
  59. >> TIR_BITS_TQ3_SH_BIG;
  60. }
  61. else
  62. {
  63. intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
  64. intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
  65. intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
  66. >> TIR_BITS1_BT_SH_LITTLE;
  67. intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
  68. >> TIR_BITS_TQ4_SH_LITTLE;
  69. intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
  70. >> TIR_BITS_TQ5_SH_LITTLE;
  71. intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
  72. >> TIR_BITS_TQ0_SH_LITTLE;
  73. intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
  74. >> TIR_BITS_TQ1_SH_LITTLE;
  75. intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
  76. >> TIR_BITS_TQ2_SH_LITTLE;
  77. intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
  78. >> TIR_BITS_TQ3_SH_LITTLE;
  79. }
  80. #ifdef TEST
  81. if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
  82. abort ();
  83. #endif
  84. }
  85. /* Swap out a type information record.
  86. BIGEND says whether AUX symbols are big-endian or little-endian; this
  87. info comes from the file header record (fh-fBigendian). */
  88. void
  89. _bfd_ecoff_swap_tir_out (int bigend,
  90. const TIR *intern_copy,
  91. struct tir_ext *ext)
  92. {
  93. TIR intern[1];
  94. *intern = *intern_copy; /* Make it reasonable to do in-place. */
  95. /* now the fun stuff... */
  96. if (bigend)
  97. {
  98. ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
  99. | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
  100. | ((intern->bt << TIR_BITS1_BT_SH_BIG)
  101. & TIR_BITS1_BT_BIG));
  102. ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
  103. & TIR_BITS_TQ4_BIG)
  104. | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
  105. & TIR_BITS_TQ5_BIG));
  106. ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
  107. & TIR_BITS_TQ0_BIG)
  108. | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
  109. & TIR_BITS_TQ1_BIG));
  110. ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
  111. & TIR_BITS_TQ2_BIG)
  112. | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
  113. & TIR_BITS_TQ3_BIG));
  114. }
  115. else
  116. {
  117. ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
  118. | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
  119. | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
  120. & TIR_BITS1_BT_LITTLE));
  121. ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
  122. & TIR_BITS_TQ4_LITTLE)
  123. | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
  124. & TIR_BITS_TQ5_LITTLE));
  125. ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
  126. & TIR_BITS_TQ0_LITTLE)
  127. | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
  128. & TIR_BITS_TQ1_LITTLE));
  129. ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
  130. & TIR_BITS_TQ2_LITTLE)
  131. | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
  132. & TIR_BITS_TQ3_LITTLE));
  133. }
  134. #ifdef TEST
  135. if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
  136. abort ();
  137. #endif
  138. }
  139. /* Swap in a relative symbol record. BIGEND says whether it is in
  140. big-endian or little-endian format.*/
  141. void
  142. _bfd_ecoff_swap_rndx_in (int bigend,
  143. const struct rndx_ext *ext_copy,
  144. RNDXR *intern)
  145. {
  146. struct rndx_ext ext[1];
  147. *ext = *ext_copy; /* Make it reasonable to do in-place. */
  148. /* now the fun stuff... */
  149. if (bigend)
  150. {
  151. intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
  152. | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
  153. >> RNDX_BITS1_RFD_SH_BIG);
  154. intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
  155. << RNDX_BITS1_INDEX_SH_LEFT_BIG)
  156. | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
  157. | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
  158. }
  159. else
  160. {
  161. intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
  162. | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
  163. << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
  164. intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
  165. >> RNDX_BITS1_INDEX_SH_LITTLE)
  166. | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
  167. | ((unsigned int) ext->r_bits[3]
  168. << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
  169. }
  170. #ifdef TEST
  171. if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
  172. abort ();
  173. #endif
  174. }
  175. /* Swap out a relative symbol record. BIGEND says whether it is in
  176. big-endian or little-endian format.*/
  177. void
  178. _bfd_ecoff_swap_rndx_out (int bigend,
  179. const RNDXR *intern_copy,
  180. struct rndx_ext *ext)
  181. {
  182. RNDXR intern[1];
  183. *intern = *intern_copy; /* Make it reasonable to do in-place. */
  184. /* now the fun stuff... */
  185. if (bigend)
  186. {
  187. ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
  188. ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
  189. & RNDX_BITS1_RFD_BIG)
  190. | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
  191. & RNDX_BITS1_INDEX_BIG));
  192. ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
  193. ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
  194. }
  195. else
  196. {
  197. ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
  198. ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
  199. & RNDX_BITS1_RFD_LITTLE)
  200. | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
  201. & RNDX_BITS1_INDEX_LITTLE));
  202. ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
  203. ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
  204. }
  205. #ifdef TEST
  206. if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
  207. abort ();
  208. #endif
  209. }
  210. /* The minimum amount of data to allocate. */
  211. #define ALLOC_SIZE (4064)
  212. /* Add bytes to a buffer. Return success. */
  213. static bfd_boolean
  214. ecoff_add_bytes (char **buf, char **bufend, size_t need)
  215. {
  216. size_t have;
  217. size_t want;
  218. char *newbuf;
  219. have = *bufend - *buf;
  220. if (have > need)
  221. want = ALLOC_SIZE;
  222. else
  223. {
  224. want = need - have;
  225. if (want < ALLOC_SIZE)
  226. want = ALLOC_SIZE;
  227. }
  228. newbuf = (char *) bfd_realloc (*buf, (bfd_size_type) have + want);
  229. if (newbuf == NULL)
  230. return FALSE;
  231. *buf = newbuf;
  232. *bufend = *buf + have + want;
  233. return TRUE;
  234. }
  235. /* We keep a hash table which maps strings to numbers. We use it to
  236. map FDR names to indices in the output file, and to map local
  237. strings when combining stabs debugging information. */
  238. struct string_hash_entry
  239. {
  240. struct bfd_hash_entry root;
  241. /* FDR index or string table offset. */
  242. long val;
  243. /* Next entry in string table. */
  244. struct string_hash_entry *next;
  245. };
  246. struct string_hash_table
  247. {
  248. struct bfd_hash_table table;
  249. };
  250. /* Routine to create an entry in a string hash table. */
  251. static struct bfd_hash_entry *
  252. string_hash_newfunc (struct bfd_hash_entry *entry,
  253. struct bfd_hash_table *table,
  254. const char *string)
  255. {
  256. struct string_hash_entry *ret = (struct string_hash_entry *) entry;
  257. /* Allocate the structure if it has not already been allocated by a
  258. subclass. */
  259. if (ret == (struct string_hash_entry *) NULL)
  260. ret = ((struct string_hash_entry *)
  261. bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
  262. if (ret == (struct string_hash_entry *) NULL)
  263. return NULL;
  264. /* Call the allocation method of the superclass. */
  265. ret = ((struct string_hash_entry *)
  266. bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  267. if (ret)
  268. {
  269. /* Initialize the local fields. */
  270. ret->val = -1;
  271. ret->next = NULL;
  272. }
  273. return (struct bfd_hash_entry *) ret;
  274. }
  275. /* Look up an entry in an string hash table. */
  276. #define string_hash_lookup(t, string, create, copy) \
  277. ((struct string_hash_entry *) \
  278. bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
  279. /* We can't afford to read in all the debugging information when we do
  280. a link. Instead, we build a list of these structures to show how
  281. different parts of the input file map to the output file. */
  282. struct shuffle
  283. {
  284. /* The next entry in this linked list. */
  285. struct shuffle *next;
  286. /* The length of the information. */
  287. unsigned long size;
  288. /* Whether this information comes from a file or not. */
  289. bfd_boolean filep;
  290. union
  291. {
  292. struct
  293. {
  294. /* The BFD the data comes from. */
  295. bfd *input_bfd;
  296. /* The offset within input_bfd. */
  297. file_ptr offset;
  298. } file;
  299. /* The data to be written out. */
  300. void * memory;
  301. } u;
  302. };
  303. /* This structure holds information across calls to
  304. bfd_ecoff_debug_accumulate. */
  305. struct accumulate
  306. {
  307. /* The FDR hash table. */
  308. struct string_hash_table fdr_hash;
  309. /* The strings hash table. */
  310. struct string_hash_table str_hash;
  311. /* Linked lists describing how to shuffle the input debug
  312. information into the output file. We keep a pointer to both the
  313. head and the tail. */
  314. struct shuffle *line;
  315. struct shuffle *line_end;
  316. struct shuffle *pdr;
  317. struct shuffle *pdr_end;
  318. struct shuffle *sym;
  319. struct shuffle *sym_end;
  320. struct shuffle *opt;
  321. struct shuffle *opt_end;
  322. struct shuffle *aux;
  323. struct shuffle *aux_end;
  324. struct shuffle *ss;
  325. struct shuffle *ss_end;
  326. struct string_hash_entry *ss_hash;
  327. struct string_hash_entry *ss_hash_end;
  328. struct shuffle *fdr;
  329. struct shuffle *fdr_end;
  330. struct shuffle *rfd;
  331. struct shuffle *rfd_end;
  332. /* The size of the largest file shuffle. */
  333. unsigned long largest_file_shuffle;
  334. /* An objalloc for debugging information. */
  335. struct objalloc *memory;
  336. };
  337. /* Add a file entry to a shuffle list. */
  338. static bfd_boolean
  339. add_file_shuffle (struct accumulate *ainfo,
  340. struct shuffle **head,
  341. struct shuffle **tail,
  342. bfd *input_bfd,
  343. file_ptr offset,
  344. unsigned long size)
  345. {
  346. struct shuffle *n;
  347. if (*tail != (struct shuffle *) NULL
  348. && (*tail)->filep
  349. && (*tail)->u.file.input_bfd == input_bfd
  350. && (*tail)->u.file.offset + (*tail)->size == (unsigned long) offset)
  351. {
  352. /* Just merge this entry onto the existing one. */
  353. (*tail)->size += size;
  354. if ((*tail)->size > ainfo->largest_file_shuffle)
  355. ainfo->largest_file_shuffle = (*tail)->size;
  356. return TRUE;
  357. }
  358. n = (struct shuffle *) objalloc_alloc (ainfo->memory,
  359. sizeof (struct shuffle));
  360. if (!n)
  361. {
  362. bfd_set_error (bfd_error_no_memory);
  363. return FALSE;
  364. }
  365. n->next = NULL;
  366. n->size = size;
  367. n->filep = TRUE;
  368. n->u.file.input_bfd = input_bfd;
  369. n->u.file.offset = offset;
  370. if (*head == (struct shuffle *) NULL)
  371. *head = n;
  372. if (*tail != (struct shuffle *) NULL)
  373. (*tail)->next = n;
  374. *tail = n;
  375. if (size > ainfo->largest_file_shuffle)
  376. ainfo->largest_file_shuffle = size;
  377. return TRUE;
  378. }
  379. /* Add a memory entry to a shuffle list. */
  380. static bfd_boolean
  381. add_memory_shuffle (struct accumulate *ainfo,
  382. struct shuffle **head,
  383. struct shuffle **tail,
  384. bfd_byte *data,
  385. unsigned long size)
  386. {
  387. struct shuffle *n;
  388. n = (struct shuffle *) objalloc_alloc (ainfo->memory,
  389. sizeof (struct shuffle));
  390. if (!n)
  391. {
  392. bfd_set_error (bfd_error_no_memory);
  393. return FALSE;
  394. }
  395. n->next = NULL;
  396. n->size = size;
  397. n->filep = FALSE;
  398. n->u.memory = data;
  399. if (*head == (struct shuffle *) NULL)
  400. *head = n;
  401. if (*tail != (struct shuffle *) NULL)
  402. (*tail)->next = n;
  403. *tail = n;
  404. return TRUE;
  405. }
  406. /* Initialize the FDR hash table. This returns a handle which is then
  407. passed in to bfd_ecoff_debug_accumulate, et. al. */
  408. void *
  409. bfd_ecoff_debug_init (bfd *output_bfd ATTRIBUTE_UNUSED,
  410. struct ecoff_debug_info *output_debug,
  411. const struct ecoff_debug_swap *output_swap ATTRIBUTE_UNUSED,
  412. struct bfd_link_info *info)
  413. {
  414. struct accumulate *ainfo;
  415. bfd_size_type amt = sizeof (struct accumulate);
  416. ainfo = (struct accumulate *) bfd_malloc (amt);
  417. if (!ainfo)
  418. return NULL;
  419. if (!bfd_hash_table_init_n (&ainfo->fdr_hash.table, string_hash_newfunc,
  420. sizeof (struct string_hash_entry), 1021))
  421. return NULL;
  422. ainfo->line = NULL;
  423. ainfo->line_end = NULL;
  424. ainfo->pdr = NULL;
  425. ainfo->pdr_end = NULL;
  426. ainfo->sym = NULL;
  427. ainfo->sym_end = NULL;
  428. ainfo->opt = NULL;
  429. ainfo->opt_end = NULL;
  430. ainfo->aux = NULL;
  431. ainfo->aux_end = NULL;
  432. ainfo->ss = NULL;
  433. ainfo->ss_end = NULL;
  434. ainfo->ss_hash = NULL;
  435. ainfo->ss_hash_end = NULL;
  436. ainfo->fdr = NULL;
  437. ainfo->fdr_end = NULL;
  438. ainfo->rfd = NULL;
  439. ainfo->rfd_end = NULL;
  440. ainfo->largest_file_shuffle = 0;
  441. if (! bfd_link_relocatable (info))
  442. {
  443. if (!bfd_hash_table_init (&ainfo->str_hash.table, string_hash_newfunc,
  444. sizeof (struct string_hash_entry)))
  445. return NULL;
  446. /* The first entry in the string table is the empty string. */
  447. output_debug->symbolic_header.issMax = 1;
  448. }
  449. ainfo->memory = objalloc_create ();
  450. if (ainfo->memory == NULL)
  451. {
  452. bfd_set_error (bfd_error_no_memory);
  453. return NULL;
  454. }
  455. return ainfo;
  456. }
  457. /* Free the accumulated debugging information. */
  458. void
  459. bfd_ecoff_debug_free (void * handle,
  460. bfd *output_bfd ATTRIBUTE_UNUSED,
  461. struct ecoff_debug_info *output_debug ATTRIBUTE_UNUSED,
  462. const struct ecoff_debug_swap *output_swap ATTRIBUTE_UNUSED,
  463. struct bfd_link_info *info)
  464. {
  465. struct accumulate *ainfo = (struct accumulate *) handle;
  466. bfd_hash_table_free (&ainfo->fdr_hash.table);
  467. if (! bfd_link_relocatable (info))
  468. bfd_hash_table_free (&ainfo->str_hash.table);
  469. objalloc_free (ainfo->memory);
  470. free (ainfo);
  471. }
  472. /* Accumulate the debugging information from INPUT_BFD into
  473. OUTPUT_BFD. The INPUT_DEBUG argument points to some ECOFF
  474. debugging information which we want to link into the information
  475. pointed to by the OUTPUT_DEBUG argument. OUTPUT_SWAP and
  476. INPUT_SWAP point to the swapping information needed. INFO is the
  477. linker information structure. HANDLE is returned by
  478. bfd_ecoff_debug_init. */
  479. bfd_boolean
  480. bfd_ecoff_debug_accumulate (void * handle,
  481. bfd *output_bfd,
  482. struct ecoff_debug_info *output_debug,
  483. const struct ecoff_debug_swap *output_swap,
  484. bfd *input_bfd,
  485. struct ecoff_debug_info *input_debug,
  486. const struct ecoff_debug_swap *input_swap,
  487. struct bfd_link_info *info)
  488. {
  489. struct accumulate *ainfo = (struct accumulate *) handle;
  490. void (* const swap_sym_in) (bfd *, void *, SYMR *)
  491. = input_swap->swap_sym_in;
  492. void (* const swap_rfd_in) (bfd *, void *, RFDT *)
  493. = input_swap->swap_rfd_in;
  494. void (* const swap_sym_out) (bfd *, const SYMR *, void *)
  495. = output_swap->swap_sym_out;
  496. void (* const swap_fdr_out) (bfd *, const FDR *, void *)
  497. = output_swap->swap_fdr_out;
  498. void (* const swap_rfd_out) (bfd *, const RFDT *, void *)
  499. = output_swap->swap_rfd_out;
  500. bfd_size_type external_pdr_size = output_swap->external_pdr_size;
  501. bfd_size_type external_sym_size = output_swap->external_sym_size;
  502. bfd_size_type external_opt_size = output_swap->external_opt_size;
  503. bfd_size_type external_fdr_size = output_swap->external_fdr_size;
  504. bfd_size_type external_rfd_size = output_swap->external_rfd_size;
  505. HDRR * const output_symhdr = &output_debug->symbolic_header;
  506. HDRR * const input_symhdr = &input_debug->symbolic_header;
  507. bfd_vma section_adjust[scMax];
  508. asection *sec;
  509. bfd_byte *fdr_start;
  510. bfd_byte *fdr_ptr;
  511. bfd_byte *fdr_end;
  512. bfd_size_type fdr_add;
  513. unsigned int copied;
  514. RFDT i;
  515. unsigned long sz;
  516. bfd_byte *rfd_out;
  517. bfd_byte *rfd_in;
  518. bfd_byte *rfd_end;
  519. long newrfdbase = 0;
  520. long oldrfdbase = 0;
  521. bfd_byte *fdr_out;
  522. bfd_size_type amt;
  523. /* Use section_adjust to hold the value to add to a symbol in a
  524. particular section. */
  525. memset (section_adjust, 0, sizeof section_adjust);
  526. #define SET(name, indx) \
  527. sec = bfd_get_section_by_name (input_bfd, name); \
  528. if (sec != NULL) \
  529. section_adjust[indx] = (sec->output_section->vma \
  530. + sec->output_offset \
  531. - sec->vma);
  532. SET (".text", scText);
  533. SET (".data", scData);
  534. SET (".bss", scBss);
  535. SET (".sdata", scSData);
  536. SET (".sbss", scSBss);
  537. /* scRdata section may be either .rdata or .rodata. */
  538. SET (".rdata", scRData);
  539. SET (".rodata", scRData);
  540. SET (".init", scInit);
  541. SET (".fini", scFini);
  542. SET (".rconst", scRConst);
  543. #undef SET
  544. /* Find all the debugging information based on the FDR's. We need
  545. to handle them whether they are swapped or not. */
  546. if (input_debug->fdr != (FDR *) NULL)
  547. {
  548. fdr_start = (bfd_byte *) input_debug->fdr;
  549. fdr_add = sizeof (FDR);
  550. }
  551. else
  552. {
  553. fdr_start = (bfd_byte *) input_debug->external_fdr;
  554. fdr_add = input_swap->external_fdr_size;
  555. }
  556. fdr_end = fdr_start + input_symhdr->ifdMax * fdr_add;
  557. amt = input_symhdr->ifdMax;
  558. amt *= sizeof (RFDT);
  559. input_debug->ifdmap = (RFDT *) bfd_alloc (input_bfd, amt);
  560. sz = (input_symhdr->crfd + input_symhdr->ifdMax) * external_rfd_size;
  561. rfd_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
  562. if (!input_debug->ifdmap || !rfd_out)
  563. {
  564. bfd_set_error (bfd_error_no_memory);
  565. return FALSE;
  566. }
  567. if (!add_memory_shuffle (ainfo, &ainfo->rfd, &ainfo->rfd_end, rfd_out, sz))
  568. return FALSE;
  569. copied = 0;
  570. /* Look through the FDR's to see which ones we are going to include
  571. in the final output. We do not want duplicate FDR information
  572. for header files, because ECOFF debugging is often very large.
  573. When we find an FDR with no line information which can be merged,
  574. we look it up in a hash table to ensure that we only include it
  575. once. We keep a table mapping FDR numbers to the final number
  576. they get with the BFD, so that we can refer to it when we write
  577. out the external symbols. */
  578. for (fdr_ptr = fdr_start, i = 0;
  579. fdr_ptr < fdr_end;
  580. fdr_ptr += fdr_add, i++, rfd_out += external_rfd_size)
  581. {
  582. FDR fdr;
  583. if (input_debug->fdr != (FDR *) NULL)
  584. fdr = *(FDR *) fdr_ptr;
  585. else
  586. (*input_swap->swap_fdr_in) (input_bfd, fdr_ptr, &fdr);
  587. /* See if this FDR can be merged with an existing one. */
  588. if (fdr.cbLine == 0 && fdr.rss != -1 && fdr.fMerge)
  589. {
  590. const char *name;
  591. char *lookup;
  592. struct string_hash_entry *fh;
  593. /* We look up a string formed from the file name and the
  594. number of symbols and aux entries. Sometimes an include
  595. file will conditionally define a typedef or something
  596. based on the order of include files. Using the number of
  597. symbols and aux entries as a hash reduces the chance that
  598. we will merge symbol information that should not be
  599. merged. */
  600. name = input_debug->ss + fdr.issBase + fdr.rss;
  601. lookup = (char *) bfd_malloc ((bfd_size_type) strlen (name) + 20);
  602. if (lookup == NULL)
  603. return FALSE;
  604. sprintf (lookup, "%s %lx %lx", name, (unsigned long) fdr.csym,
  605. (unsigned long) fdr.caux);
  606. fh = string_hash_lookup (&ainfo->fdr_hash, lookup, TRUE, TRUE);
  607. free (lookup);
  608. if (fh == (struct string_hash_entry *) NULL)
  609. return FALSE;
  610. if (fh->val != -1)
  611. {
  612. input_debug->ifdmap[i] = fh->val;
  613. (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i, rfd_out);
  614. /* Don't copy this FDR. */
  615. continue;
  616. }
  617. fh->val = output_symhdr->ifdMax + copied;
  618. }
  619. input_debug->ifdmap[i] = output_symhdr->ifdMax + copied;
  620. (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i, rfd_out);
  621. ++copied;
  622. }
  623. newrfdbase = output_symhdr->crfd;
  624. output_symhdr->crfd += input_symhdr->ifdMax;
  625. /* Copy over any existing RFD's. RFD's are only created by the
  626. linker, so this will only happen for input files which are the
  627. result of a partial link. */
  628. rfd_in = (bfd_byte *) input_debug->external_rfd;
  629. rfd_end = rfd_in + input_symhdr->crfd * input_swap->external_rfd_size;
  630. for (;
  631. rfd_in < rfd_end;
  632. rfd_in += input_swap->external_rfd_size)
  633. {
  634. RFDT rfd;
  635. (*swap_rfd_in) (input_bfd, rfd_in, &rfd);
  636. BFD_ASSERT (rfd >= 0 && rfd < input_symhdr->ifdMax);
  637. rfd = input_debug->ifdmap[rfd];
  638. (*swap_rfd_out) (output_bfd, &rfd, rfd_out);
  639. rfd_out += external_rfd_size;
  640. }
  641. oldrfdbase = output_symhdr->crfd;
  642. output_symhdr->crfd += input_symhdr->crfd;
  643. /* Look through the FDR's and copy over all associated debugging
  644. information. */
  645. sz = copied * external_fdr_size;
  646. fdr_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
  647. if (!fdr_out)
  648. {
  649. bfd_set_error (bfd_error_no_memory);
  650. return FALSE;
  651. }
  652. if (!add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end, fdr_out, sz))
  653. return FALSE;
  654. for (fdr_ptr = fdr_start, i = 0;
  655. fdr_ptr < fdr_end;
  656. fdr_ptr += fdr_add, i++)
  657. {
  658. FDR fdr;
  659. bfd_byte *sym_out;
  660. bfd_byte *lraw_src;
  661. bfd_byte *lraw_end;
  662. bfd_boolean fgotfilename;
  663. if (input_debug->ifdmap[i] < output_symhdr->ifdMax)
  664. {
  665. /* We are not copying this FDR. */
  666. continue;
  667. }
  668. if (input_debug->fdr != (FDR *) NULL)
  669. fdr = *(FDR *) fdr_ptr;
  670. else
  671. (*input_swap->swap_fdr_in) (input_bfd, fdr_ptr, &fdr);
  672. /* FIXME: It is conceivable that this FDR points to the .init or
  673. .fini section, in which case this will not do the right
  674. thing. */
  675. fdr.adr += section_adjust[scText];
  676. /* Swap in the local symbols, adjust their values, and swap them
  677. out again. */
  678. fgotfilename = FALSE;
  679. sz = fdr.csym * external_sym_size;
  680. sym_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
  681. if (!sym_out)
  682. {
  683. bfd_set_error (bfd_error_no_memory);
  684. return FALSE;
  685. }
  686. if (!add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end, sym_out,
  687. sz))
  688. return FALSE;
  689. lraw_src = ((bfd_byte *) input_debug->external_sym
  690. + fdr.isymBase * input_swap->external_sym_size);
  691. lraw_end = lraw_src + fdr.csym * input_swap->external_sym_size;
  692. for (; lraw_src < lraw_end; lraw_src += input_swap->external_sym_size)
  693. {
  694. SYMR internal_sym;
  695. (*swap_sym_in) (input_bfd, lraw_src, &internal_sym);
  696. BFD_ASSERT (internal_sym.sc != scCommon
  697. && internal_sym.sc != scSCommon);
  698. /* Adjust the symbol value if appropriate. */
  699. switch (internal_sym.st)
  700. {
  701. case stNil:
  702. if (ECOFF_IS_STAB (&internal_sym))
  703. break;
  704. /* Fall through. */
  705. case stGlobal:
  706. case stStatic:
  707. case stLabel:
  708. case stProc:
  709. case stStaticProc:
  710. internal_sym.value += section_adjust[internal_sym.sc];
  711. break;
  712. default:
  713. break;
  714. }
  715. /* If we are doing a final link, we hash all the strings in
  716. the local symbol table together. This reduces the amount
  717. of space required by debugging information. We don't do
  718. this when performing a relocatable link because it would
  719. prevent us from easily merging different FDR's. */
  720. if (! bfd_link_relocatable (info))
  721. {
  722. bfd_boolean ffilename;
  723. const char *name;
  724. if (! fgotfilename && internal_sym.iss == fdr.rss)
  725. ffilename = TRUE;
  726. else
  727. ffilename = FALSE;
  728. /* Hash the name into the string table. */
  729. name = input_debug->ss + fdr.issBase + internal_sym.iss;
  730. if (*name == '\0')
  731. internal_sym.iss = 0;
  732. else
  733. {
  734. struct string_hash_entry *sh;
  735. sh = string_hash_lookup (&ainfo->str_hash, name, TRUE, TRUE);
  736. if (sh == (struct string_hash_entry *) NULL)
  737. return FALSE;
  738. if (sh->val == -1)
  739. {
  740. sh->val = output_symhdr->issMax;
  741. output_symhdr->issMax += strlen (name) + 1;
  742. if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
  743. ainfo->ss_hash = sh;
  744. if (ainfo->ss_hash_end
  745. != (struct string_hash_entry *) NULL)
  746. ainfo->ss_hash_end->next = sh;
  747. ainfo->ss_hash_end = sh;
  748. }
  749. internal_sym.iss = sh->val;
  750. }
  751. if (ffilename)
  752. {
  753. fdr.rss = internal_sym.iss;
  754. fgotfilename = TRUE;
  755. }
  756. }
  757. (*swap_sym_out) (output_bfd, &internal_sym, sym_out);
  758. sym_out += external_sym_size;
  759. }
  760. fdr.isymBase = output_symhdr->isymMax;
  761. output_symhdr->isymMax += fdr.csym;
  762. /* Copy the information that does not need swapping. */
  763. /* FIXME: If we are relaxing, we need to adjust the line
  764. numbers. Frankly, forget it. Anybody using stabs debugging
  765. information will not use this line number information, and
  766. stabs are adjusted correctly. */
  767. if (fdr.cbLine > 0)
  768. {
  769. file_ptr pos = input_symhdr->cbLineOffset + fdr.cbLineOffset;
  770. if (!add_file_shuffle (ainfo, &ainfo->line, &ainfo->line_end,
  771. input_bfd, pos, (unsigned long) fdr.cbLine))
  772. return FALSE;
  773. fdr.ilineBase = output_symhdr->ilineMax;
  774. fdr.cbLineOffset = output_symhdr->cbLine;
  775. output_symhdr->ilineMax += fdr.cline;
  776. output_symhdr->cbLine += fdr.cbLine;
  777. }
  778. if (fdr.caux > 0)
  779. {
  780. file_ptr pos = (input_symhdr->cbAuxOffset
  781. + fdr.iauxBase * sizeof (union aux_ext));
  782. if (!add_file_shuffle (ainfo, &ainfo->aux, &ainfo->aux_end,
  783. input_bfd, pos,
  784. fdr.caux * sizeof (union aux_ext)))
  785. return FALSE;
  786. fdr.iauxBase = output_symhdr->iauxMax;
  787. output_symhdr->iauxMax += fdr.caux;
  788. }
  789. if (! bfd_link_relocatable (info))
  790. {
  791. /* When are are hashing strings, we lie about the number of
  792. strings attached to each FDR. We need to set cbSs
  793. because some versions of dbx apparently use it to decide
  794. how much of the string table to read in. */
  795. fdr.issBase = 0;
  796. fdr.cbSs = output_symhdr->issMax;
  797. }
  798. else if (fdr.cbSs > 0)
  799. {
  800. file_ptr pos = input_symhdr->cbSsOffset + fdr.issBase;
  801. if (!add_file_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end,
  802. input_bfd, pos, (unsigned long) fdr.cbSs))
  803. return FALSE;
  804. fdr.issBase = output_symhdr->issMax;
  805. output_symhdr->issMax += fdr.cbSs;
  806. }
  807. if (output_bfd->xvec->header_byteorder
  808. == input_bfd->xvec->header_byteorder)
  809. {
  810. /* The two BFD's have the same endianness, and we don't have
  811. to adjust the PDR addresses, so simply copying the
  812. information will suffice. */
  813. BFD_ASSERT (external_pdr_size == input_swap->external_pdr_size);
  814. if (fdr.cpd > 0)
  815. {
  816. file_ptr pos = (input_symhdr->cbPdOffset
  817. + fdr.ipdFirst * external_pdr_size);
  818. unsigned long size = fdr.cpd * external_pdr_size;
  819. if (!add_file_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end,
  820. input_bfd, pos, size))
  821. return FALSE;
  822. }
  823. BFD_ASSERT (external_opt_size == input_swap->external_opt_size);
  824. if (fdr.copt > 0)
  825. {
  826. file_ptr pos = (input_symhdr->cbOptOffset
  827. + fdr.ioptBase * external_opt_size);
  828. unsigned long size = fdr.copt * external_opt_size;
  829. if (!add_file_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end,
  830. input_bfd, pos, size))
  831. return FALSE;
  832. }
  833. }
  834. else
  835. {
  836. bfd_size_type outsz, insz;
  837. bfd_byte *in;
  838. bfd_byte *end;
  839. bfd_byte *out;
  840. /* The two BFD's have different endianness, so we must swap
  841. everything in and out. This code would always work, but
  842. it would be unnecessarily slow in the normal case. */
  843. outsz = external_pdr_size;
  844. insz = input_swap->external_pdr_size;
  845. in = ((bfd_byte *) input_debug->external_pdr
  846. + fdr.ipdFirst * insz);
  847. end = in + fdr.cpd * insz;
  848. sz = fdr.cpd * outsz;
  849. out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
  850. if (!out)
  851. {
  852. bfd_set_error (bfd_error_no_memory);
  853. return FALSE;
  854. }
  855. if (!add_memory_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end, out,
  856. sz))
  857. return FALSE;
  858. for (; in < end; in += insz, out += outsz)
  859. {
  860. PDR pdr;
  861. (*input_swap->swap_pdr_in) (input_bfd, in, &pdr);
  862. (*output_swap->swap_pdr_out) (output_bfd, &pdr, out);
  863. }
  864. /* Swap over the optimization information. */
  865. outsz = external_opt_size;
  866. insz = input_swap->external_opt_size;
  867. in = ((bfd_byte *) input_debug->external_opt
  868. + fdr.ioptBase * insz);
  869. end = in + fdr.copt * insz;
  870. sz = fdr.copt * outsz;
  871. out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
  872. if (!out)
  873. {
  874. bfd_set_error (bfd_error_no_memory);
  875. return FALSE;
  876. }
  877. if (!add_memory_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end, out,
  878. sz))
  879. return FALSE;
  880. for (; in < end; in += insz, out += outsz)
  881. {
  882. OPTR opt;
  883. (*input_swap->swap_opt_in) (input_bfd, in, &opt);
  884. (*output_swap->swap_opt_out) (output_bfd, &opt, out);
  885. }
  886. }
  887. fdr.ipdFirst = output_symhdr->ipdMax;
  888. output_symhdr->ipdMax += fdr.cpd;
  889. fdr.ioptBase = output_symhdr->ioptMax;
  890. output_symhdr->ioptMax += fdr.copt;
  891. if (fdr.crfd <= 0)
  892. {
  893. /* Point this FDR at the table of RFD's we created. */
  894. fdr.rfdBase = newrfdbase;
  895. fdr.crfd = input_symhdr->ifdMax;
  896. }
  897. else
  898. {
  899. /* Point this FDR at the remapped RFD's. */
  900. fdr.rfdBase += oldrfdbase;
  901. }
  902. (*swap_fdr_out) (output_bfd, &fdr, fdr_out);
  903. fdr_out += external_fdr_size;
  904. ++output_symhdr->ifdMax;
  905. }
  906. return TRUE;
  907. }
  908. /* Add a string to the debugging information we are accumulating.
  909. Return the offset from the fdr string base. */
  910. static long
  911. ecoff_add_string (struct accumulate *ainfo,
  912. struct bfd_link_info *info,
  913. struct ecoff_debug_info *debug,
  914. FDR *fdr,
  915. const char *string)
  916. {
  917. HDRR *symhdr;
  918. size_t len;
  919. bfd_size_type ret;
  920. symhdr = &debug->symbolic_header;
  921. len = strlen (string);
  922. if (bfd_link_relocatable (info))
  923. {
  924. if (!add_memory_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end,
  925. (bfd_byte *) string, len + 1))
  926. return -1;
  927. ret = symhdr->issMax;
  928. symhdr->issMax += len + 1;
  929. fdr->cbSs += len + 1;
  930. }
  931. else
  932. {
  933. struct string_hash_entry *sh;
  934. sh = string_hash_lookup (&ainfo->str_hash, string, TRUE, TRUE);
  935. if (sh == (struct string_hash_entry *) NULL)
  936. return -1;
  937. if (sh->val == -1)
  938. {
  939. sh->val = symhdr->issMax;
  940. symhdr->issMax += len + 1;
  941. if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
  942. ainfo->ss_hash = sh;
  943. if (ainfo->ss_hash_end
  944. != (struct string_hash_entry *) NULL)
  945. ainfo->ss_hash_end->next = sh;
  946. ainfo->ss_hash_end = sh;
  947. }
  948. ret = sh->val;
  949. }
  950. return ret;
  951. }
  952. /* Add debugging information from a non-ECOFF file. */
  953. bfd_boolean
  954. bfd_ecoff_debug_accumulate_other (void * handle,
  955. bfd *output_bfd,
  956. struct ecoff_debug_info *output_debug,
  957. const struct ecoff_debug_swap *output_swap,
  958. bfd *input_bfd,
  959. struct bfd_link_info *info)
  960. {
  961. struct accumulate *ainfo = (struct accumulate *) handle;
  962. void (* const swap_sym_out) (bfd *, const SYMR *, void *)
  963. = output_swap->swap_sym_out;
  964. HDRR *output_symhdr = &output_debug->symbolic_header;
  965. FDR fdr;
  966. asection *sec;
  967. asymbol **symbols;
  968. asymbol **sym_ptr;
  969. asymbol **sym_end;
  970. long symsize;
  971. long symcount;
  972. void * external_fdr;
  973. memset (&fdr, 0, sizeof fdr);
  974. sec = bfd_get_section_by_name (input_bfd, ".text");
  975. if (sec != NULL)
  976. fdr.adr = sec->output_section->vma + sec->output_offset;
  977. else
  978. {
  979. /* FIXME: What about .init or .fini? */
  980. fdr.adr = 0;
  981. }
  982. fdr.issBase = output_symhdr->issMax;
  983. fdr.cbSs = 0;
  984. fdr.rss = ecoff_add_string (ainfo, info, output_debug, &fdr,
  985. input_bfd->filename);
  986. if (fdr.rss == -1)
  987. return FALSE;
  988. fdr.isymBase = output_symhdr->isymMax;
  989. /* Get the local symbols from the input BFD. */
  990. symsize = bfd_get_symtab_upper_bound (input_bfd);
  991. if (symsize < 0)
  992. return FALSE;
  993. symbols = (asymbol **) bfd_alloc (output_bfd, (bfd_size_type) symsize);
  994. if (symbols == (asymbol **) NULL)
  995. return FALSE;
  996. symcount = bfd_canonicalize_symtab (input_bfd, symbols);
  997. if (symcount < 0)
  998. return FALSE;
  999. sym_end = symbols + symcount;
  1000. /* Handle the local symbols. Any external symbols are handled
  1001. separately. */
  1002. fdr.csym = 0;
  1003. for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
  1004. {
  1005. SYMR internal_sym;
  1006. void * external_sym;
  1007. if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
  1008. continue;
  1009. memset (&internal_sym, 0, sizeof internal_sym);
  1010. internal_sym.iss = ecoff_add_string (ainfo, info, output_debug, &fdr,
  1011. (*sym_ptr)->name);
  1012. if (internal_sym.iss == -1)
  1013. return FALSE;
  1014. if (bfd_is_com_section ((*sym_ptr)->section)
  1015. || bfd_is_und_section ((*sym_ptr)->section))
  1016. internal_sym.value = (*sym_ptr)->value;
  1017. else
  1018. internal_sym.value = ((*sym_ptr)->value
  1019. + (*sym_ptr)->section->output_offset
  1020. + (*sym_ptr)->section->output_section->vma);
  1021. internal_sym.st = stNil;
  1022. internal_sym.sc = scUndefined;
  1023. internal_sym.index = indexNil;
  1024. external_sym = objalloc_alloc (ainfo->memory,
  1025. output_swap->external_sym_size);
  1026. if (!external_sym)
  1027. {
  1028. bfd_set_error (bfd_error_no_memory);
  1029. return FALSE;
  1030. }
  1031. (*swap_sym_out) (output_bfd, &internal_sym, external_sym);
  1032. add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end,
  1033. (bfd_byte *) external_sym,
  1034. (unsigned long) output_swap->external_sym_size);
  1035. ++fdr.csym;
  1036. ++output_symhdr->isymMax;
  1037. }
  1038. bfd_release (output_bfd, symbols);
  1039. /* Leave everything else in the FDR zeroed out. This will cause
  1040. the lang field to be langC. The fBigendian field will
  1041. indicate little endian format, but it doesn't matter because
  1042. it only applies to aux fields and there are none. */
  1043. external_fdr = objalloc_alloc (ainfo->memory,
  1044. output_swap->external_fdr_size);
  1045. if (!external_fdr)
  1046. {
  1047. bfd_set_error (bfd_error_no_memory);
  1048. return FALSE;
  1049. }
  1050. (*output_swap->swap_fdr_out) (output_bfd, &fdr, external_fdr);
  1051. add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end,
  1052. (bfd_byte *) external_fdr,
  1053. (unsigned long) output_swap->external_fdr_size);
  1054. ++output_symhdr->ifdMax;
  1055. return TRUE;
  1056. }
  1057. /* Set up ECOFF debugging information for the external symbols.
  1058. FIXME: This is done using a memory buffer, but it should be
  1059. probably be changed to use a shuffle structure. The assembler uses
  1060. this interface, so that must be changed to do something else. */
  1061. bfd_boolean
  1062. bfd_ecoff_debug_externals (bfd *abfd,
  1063. struct ecoff_debug_info *debug,
  1064. const struct ecoff_debug_swap *swap,
  1065. bfd_boolean relocatable,
  1066. bfd_boolean (*get_extr) (asymbol *, EXTR *),
  1067. void (*set_index) (asymbol *, bfd_size_type))
  1068. {
  1069. HDRR * const symhdr = &debug->symbolic_header;
  1070. asymbol **sym_ptr_ptr;
  1071. size_t c;
  1072. sym_ptr_ptr = bfd_get_outsymbols (abfd);
  1073. if (sym_ptr_ptr == NULL)
  1074. return TRUE;
  1075. for (c = bfd_get_symcount (abfd); c > 0; c--, sym_ptr_ptr++)
  1076. {
  1077. asymbol *sym_ptr;
  1078. EXTR esym;
  1079. sym_ptr = *sym_ptr_ptr;
  1080. /* Get the external symbol information. */
  1081. if (! (*get_extr) (sym_ptr, &esym))
  1082. continue;
  1083. /* If we're producing an executable, move common symbols into
  1084. bss. */
  1085. if (! relocatable)
  1086. {
  1087. if (esym.asym.sc == scCommon)
  1088. esym.asym.sc = scBss;
  1089. else if (esym.asym.sc == scSCommon)
  1090. esym.asym.sc = scSBss;
  1091. }
  1092. if (bfd_is_com_section (sym_ptr->section)
  1093. || bfd_is_und_section (sym_ptr->section)
  1094. || sym_ptr->section->output_section == (asection *) NULL)
  1095. {
  1096. /* FIXME: gas does not keep the value of a small undefined
  1097. symbol in the symbol itself, because of relocation
  1098. problems. */
  1099. if (esym.asym.sc != scSUndefined
  1100. || esym.asym.value == 0
  1101. || sym_ptr->value != 0)
  1102. esym.asym.value = sym_ptr->value;
  1103. }
  1104. else
  1105. esym.asym.value = (sym_ptr->value
  1106. + sym_ptr->section->output_offset
  1107. + sym_ptr->section->output_section->vma);
  1108. if (set_index)
  1109. (*set_index) (sym_ptr, (bfd_size_type) symhdr->iextMax);
  1110. if (! bfd_ecoff_debug_one_external (abfd, debug, swap,
  1111. sym_ptr->name, &esym))
  1112. return FALSE;
  1113. }
  1114. return TRUE;
  1115. }
  1116. /* Add a single external symbol to the debugging information. */
  1117. bfd_boolean
  1118. bfd_ecoff_debug_one_external (bfd *abfd,
  1119. struct ecoff_debug_info *debug,
  1120. const struct ecoff_debug_swap *swap,
  1121. const char *name,
  1122. EXTR *esym)
  1123. {
  1124. const bfd_size_type external_ext_size = swap->external_ext_size;
  1125. void (* const swap_ext_out) (bfd *, const EXTR *, void *)
  1126. = swap->swap_ext_out;
  1127. HDRR * const symhdr = &debug->symbolic_header;
  1128. size_t namelen;
  1129. namelen = strlen (name);
  1130. if ((size_t) (debug->ssext_end - debug->ssext)
  1131. < symhdr->issExtMax + namelen + 1)
  1132. {
  1133. if (! ecoff_add_bytes ((char **) &debug->ssext,
  1134. (char **) &debug->ssext_end,
  1135. symhdr->issExtMax + namelen + 1))
  1136. return FALSE;
  1137. }
  1138. if ((size_t) ((char *) debug->external_ext_end
  1139. - (char *) debug->external_ext)
  1140. < (symhdr->iextMax + 1) * external_ext_size)
  1141. {
  1142. char *external_ext = (char *) debug->external_ext;
  1143. char *external_ext_end = (char *) debug->external_ext_end;
  1144. if (! ecoff_add_bytes ((char **) &external_ext,
  1145. (char **) &external_ext_end,
  1146. (symhdr->iextMax + 1) * (size_t) external_ext_size))
  1147. return FALSE;
  1148. debug->external_ext = external_ext;
  1149. debug->external_ext_end = external_ext_end;
  1150. }
  1151. esym->asym.iss = symhdr->issExtMax;
  1152. (*swap_ext_out) (abfd, esym,
  1153. ((char *) debug->external_ext
  1154. + symhdr->iextMax * swap->external_ext_size));
  1155. ++symhdr->iextMax;
  1156. strcpy (debug->ssext + symhdr->issExtMax, name);
  1157. symhdr->issExtMax += namelen + 1;
  1158. return TRUE;
  1159. }
  1160. /* Align the ECOFF debugging information. */
  1161. static void
  1162. ecoff_align_debug (bfd *abfd ATTRIBUTE_UNUSED,
  1163. struct ecoff_debug_info *debug,
  1164. const struct ecoff_debug_swap *swap)
  1165. {
  1166. HDRR * const symhdr = &debug->symbolic_header;
  1167. bfd_size_type debug_align, aux_align, rfd_align;
  1168. size_t add;
  1169. /* Adjust the counts so that structures are aligned. */
  1170. debug_align = swap->debug_align;
  1171. aux_align = debug_align / sizeof (union aux_ext);
  1172. rfd_align = debug_align / swap->external_rfd_size;
  1173. add = debug_align - (symhdr->cbLine & (debug_align - 1));
  1174. if (add != debug_align)
  1175. {
  1176. if (debug->line != (unsigned char *) NULL)
  1177. memset ((debug->line + symhdr->cbLine), 0, add);
  1178. symhdr->cbLine += add;
  1179. }
  1180. add = debug_align - (symhdr->issMax & (debug_align - 1));
  1181. if (add != debug_align)
  1182. {
  1183. if (debug->ss != (char *) NULL)
  1184. memset ((debug->ss + symhdr->issMax), 0, add);
  1185. symhdr->issMax += add;
  1186. }
  1187. add = debug_align - (symhdr->issExtMax & (debug_align - 1));
  1188. if (add != debug_align)
  1189. {
  1190. if (debug->ssext != (char *) NULL)
  1191. memset ((debug->ssext + symhdr->issExtMax), 0, add);
  1192. symhdr->issExtMax += add;
  1193. }
  1194. add = aux_align - (symhdr->iauxMax & (aux_align - 1));
  1195. if (add != aux_align)
  1196. {
  1197. if (debug->external_aux != (union aux_ext *) NULL)
  1198. memset ((debug->external_aux + symhdr->iauxMax), 0,
  1199. add * sizeof (union aux_ext));
  1200. symhdr->iauxMax += add;
  1201. }
  1202. add = rfd_align - (symhdr->crfd & (rfd_align - 1));
  1203. if (add != rfd_align)
  1204. {
  1205. if (debug->external_rfd != NULL)
  1206. memset (((char *) debug->external_rfd
  1207. + symhdr->crfd * swap->external_rfd_size),
  1208. 0, (size_t) (add * swap->external_rfd_size));
  1209. symhdr->crfd += add;
  1210. }
  1211. }
  1212. /* Return the size required by the ECOFF debugging information. */
  1213. bfd_size_type
  1214. bfd_ecoff_debug_size (bfd *abfd,
  1215. struct ecoff_debug_info *debug,
  1216. const struct ecoff_debug_swap *swap)
  1217. {
  1218. bfd_size_type tot;
  1219. ecoff_align_debug (abfd, debug, swap);
  1220. tot = swap->external_hdr_size;
  1221. #define ADD(count, size) \
  1222. tot += debug->symbolic_header.count * size
  1223. ADD (cbLine, sizeof (unsigned char));
  1224. ADD (idnMax, swap->external_dnr_size);
  1225. ADD (ipdMax, swap->external_pdr_size);
  1226. ADD (isymMax, swap->external_sym_size);
  1227. ADD (ioptMax, swap->external_opt_size);
  1228. ADD (iauxMax, sizeof (union aux_ext));
  1229. ADD (issMax, sizeof (char));
  1230. ADD (issExtMax, sizeof (char));
  1231. ADD (ifdMax, swap->external_fdr_size);
  1232. ADD (crfd, swap->external_rfd_size);
  1233. ADD (iextMax, swap->external_ext_size);
  1234. #undef ADD
  1235. return tot;
  1236. }
  1237. /* Write out the ECOFF symbolic header, given the file position it is
  1238. going to be placed at. This assumes that the counts are set
  1239. correctly. */
  1240. static bfd_boolean
  1241. ecoff_write_symhdr (bfd *abfd,
  1242. struct ecoff_debug_info *debug,
  1243. const struct ecoff_debug_swap *swap,
  1244. file_ptr where)
  1245. {
  1246. HDRR * const symhdr = &debug->symbolic_header;
  1247. char *buff = NULL;
  1248. ecoff_align_debug (abfd, debug, swap);
  1249. /* Go to the right location in the file. */
  1250. if (bfd_seek (abfd, where, SEEK_SET) != 0)
  1251. return FALSE;
  1252. where += swap->external_hdr_size;
  1253. symhdr->magic = swap->sym_magic;
  1254. /* Fill in the file offsets. */
  1255. #define SET(offset, count, size) \
  1256. if (symhdr->count == 0) \
  1257. symhdr->offset = 0; \
  1258. else \
  1259. { \
  1260. symhdr->offset = where; \
  1261. where += symhdr->count * size; \
  1262. }
  1263. SET (cbLineOffset, cbLine, sizeof (unsigned char));
  1264. SET (cbDnOffset, idnMax, swap->external_dnr_size);
  1265. SET (cbPdOffset, ipdMax, swap->external_pdr_size);
  1266. SET (cbSymOffset, isymMax, swap->external_sym_size);
  1267. SET (cbOptOffset, ioptMax, swap->external_opt_size);
  1268. SET (cbAuxOffset, iauxMax, sizeof (union aux_ext));
  1269. SET (cbSsOffset, issMax, sizeof (char));
  1270. SET (cbSsExtOffset, issExtMax, sizeof (char));
  1271. SET (cbFdOffset, ifdMax, swap->external_fdr_size);
  1272. SET (cbRfdOffset, crfd, swap->external_rfd_size);
  1273. SET (cbExtOffset, iextMax, swap->external_ext_size);
  1274. #undef SET
  1275. buff = (char *) bfd_malloc (swap->external_hdr_size);
  1276. if (buff == NULL && swap->external_hdr_size != 0)
  1277. goto error_return;
  1278. (*swap->swap_hdr_out) (abfd, symhdr, buff);
  1279. if (bfd_bwrite (buff, swap->external_hdr_size, abfd)
  1280. != swap->external_hdr_size)
  1281. goto error_return;
  1282. if (buff != NULL)
  1283. free (buff);
  1284. return TRUE;
  1285. error_return:
  1286. if (buff != NULL)
  1287. free (buff);
  1288. return FALSE;
  1289. }
  1290. /* Write out the ECOFF debugging information. This function assumes
  1291. that the information (the pointers and counts) in *DEBUG have been
  1292. set correctly. WHERE is the position in the file to write the
  1293. information to. This function fills in the file offsets in the
  1294. symbolic header. */
  1295. bfd_boolean
  1296. bfd_ecoff_write_debug (bfd *abfd,
  1297. struct ecoff_debug_info *debug,
  1298. const struct ecoff_debug_swap *swap,
  1299. file_ptr where)
  1300. {
  1301. HDRR * const symhdr = &debug->symbolic_header;
  1302. if (! ecoff_write_symhdr (abfd, debug, swap, where))
  1303. return FALSE;
  1304. #define WRITE(ptr, count, size, offset) \
  1305. BFD_ASSERT (symhdr->offset == 0 \
  1306. || (bfd_vma) bfd_tell (abfd) == symhdr->offset); \
  1307. if (bfd_bwrite (debug->ptr, (bfd_size_type) size * symhdr->count, abfd)\
  1308. != size * symhdr->count) \
  1309. return FALSE;
  1310. WRITE (line, cbLine, sizeof (unsigned char), cbLineOffset);
  1311. WRITE (external_dnr, idnMax, swap->external_dnr_size, cbDnOffset);
  1312. WRITE (external_pdr, ipdMax, swap->external_pdr_size, cbPdOffset);
  1313. WRITE (external_sym, isymMax, swap->external_sym_size, cbSymOffset);
  1314. WRITE (external_opt, ioptMax, swap->external_opt_size, cbOptOffset);
  1315. WRITE (external_aux, iauxMax, (bfd_size_type) sizeof (union aux_ext),
  1316. cbAuxOffset);
  1317. WRITE (ss, issMax, sizeof (char), cbSsOffset);
  1318. WRITE (ssext, issExtMax, sizeof (char), cbSsExtOffset);
  1319. WRITE (external_fdr, ifdMax, swap->external_fdr_size, cbFdOffset);
  1320. WRITE (external_rfd, crfd, swap->external_rfd_size, cbRfdOffset);
  1321. WRITE (external_ext, iextMax, swap->external_ext_size, cbExtOffset);
  1322. #undef WRITE
  1323. return TRUE;
  1324. }
  1325. /* Write out a shuffle list. */
  1326. static bfd_boolean
  1327. ecoff_write_shuffle (bfd *abfd,
  1328. const struct ecoff_debug_swap *swap,
  1329. struct shuffle *shuffle,
  1330. void * space)
  1331. {
  1332. struct shuffle *l;
  1333. unsigned long total;
  1334. total = 0;
  1335. for (l = shuffle; l != (struct shuffle *) NULL; l = l->next)
  1336. {
  1337. if (! l->filep)
  1338. {
  1339. if (bfd_bwrite (l->u.memory, (bfd_size_type) l->size, abfd)
  1340. != l->size)
  1341. return FALSE;
  1342. }
  1343. else
  1344. {
  1345. if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
  1346. || bfd_bread (space, (bfd_size_type) l->size,
  1347. l->u.file.input_bfd) != l->size
  1348. || bfd_bwrite (space, (bfd_size_type) l->size, abfd) != l->size)
  1349. return FALSE;
  1350. }
  1351. total += l->size;
  1352. }
  1353. if ((total & (swap->debug_align - 1)) != 0)
  1354. {
  1355. unsigned int i;
  1356. bfd_byte *s;
  1357. i = swap->debug_align - (total & (swap->debug_align - 1));
  1358. s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
  1359. if (s == NULL && i != 0)
  1360. return FALSE;
  1361. if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
  1362. {
  1363. free (s);
  1364. return FALSE;
  1365. }
  1366. free (s);
  1367. }
  1368. return TRUE;
  1369. }
  1370. /* Write out debugging information using accumulated linker
  1371. information. */
  1372. bfd_boolean
  1373. bfd_ecoff_write_accumulated_debug (void * handle,
  1374. bfd *abfd,
  1375. struct ecoff_debug_info *debug,
  1376. const struct ecoff_debug_swap *swap,
  1377. struct bfd_link_info *info,
  1378. file_ptr where)
  1379. {
  1380. struct accumulate *ainfo = (struct accumulate *) handle;
  1381. void * space = NULL;
  1382. bfd_size_type amt;
  1383. if (! ecoff_write_symhdr (abfd, debug, swap, where))
  1384. goto error_return;
  1385. amt = ainfo->largest_file_shuffle;
  1386. space = bfd_malloc (amt);
  1387. if (space == NULL && ainfo->largest_file_shuffle != 0)
  1388. goto error_return;
  1389. if (! ecoff_write_shuffle (abfd, swap, ainfo->line, space)
  1390. || ! ecoff_write_shuffle (abfd, swap, ainfo->pdr, space)
  1391. || ! ecoff_write_shuffle (abfd, swap, ainfo->sym, space)
  1392. || ! ecoff_write_shuffle (abfd, swap, ainfo->opt, space)
  1393. || ! ecoff_write_shuffle (abfd, swap, ainfo->aux, space))
  1394. goto error_return;
  1395. /* The string table is written out from the hash table if this is a
  1396. final link. */
  1397. if (bfd_link_relocatable (info))
  1398. {
  1399. BFD_ASSERT (ainfo->ss_hash == (struct string_hash_entry *) NULL);
  1400. if (! ecoff_write_shuffle (abfd, swap, ainfo->ss, space))
  1401. goto error_return;
  1402. }
  1403. else
  1404. {
  1405. unsigned long total;
  1406. bfd_byte null;
  1407. struct string_hash_entry *sh;
  1408. BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
  1409. null = 0;
  1410. if (bfd_bwrite (&null, (bfd_size_type) 1, abfd) != 1)
  1411. goto error_return;
  1412. total = 1;
  1413. BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
  1414. for (sh = ainfo->ss_hash;
  1415. sh != (struct string_hash_entry *) NULL;
  1416. sh = sh->next)
  1417. {
  1418. size_t len;
  1419. len = strlen (sh->root.string);
  1420. amt = len + 1;
  1421. if (bfd_bwrite (sh->root.string, amt, abfd) != amt)
  1422. goto error_return;
  1423. total += len + 1;
  1424. }
  1425. if ((total & (swap->debug_align - 1)) != 0)
  1426. {
  1427. unsigned int i;
  1428. bfd_byte *s;
  1429. i = swap->debug_align - (total & (swap->debug_align - 1));
  1430. s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
  1431. if (s == NULL && i != 0)
  1432. goto error_return;
  1433. if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
  1434. {
  1435. free (s);
  1436. goto error_return;
  1437. }
  1438. free (s);
  1439. }
  1440. }
  1441. /* The external strings and symbol are not converted over to using
  1442. shuffles. FIXME: They probably should be. */
  1443. amt = debug->symbolic_header.issExtMax;
  1444. if (bfd_bwrite (debug->ssext, amt, abfd) != amt)
  1445. goto error_return;
  1446. if ((debug->symbolic_header.issExtMax & (swap->debug_align - 1)) != 0)
  1447. {
  1448. unsigned int i;
  1449. bfd_byte *s;
  1450. i = (swap->debug_align
  1451. - (debug->symbolic_header.issExtMax & (swap->debug_align - 1)));
  1452. s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
  1453. if (s == NULL && i != 0)
  1454. goto error_return;
  1455. if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
  1456. {
  1457. free (s);
  1458. goto error_return;
  1459. }
  1460. free (s);
  1461. }
  1462. if (! ecoff_write_shuffle (abfd, swap, ainfo->fdr, space)
  1463. || ! ecoff_write_shuffle (abfd, swap, ainfo->rfd, space))
  1464. goto error_return;
  1465. BFD_ASSERT (debug->symbolic_header.cbExtOffset == 0
  1466. || (debug->symbolic_header.cbExtOffset
  1467. == (bfd_vma) bfd_tell (abfd)));
  1468. amt = debug->symbolic_header.iextMax * swap->external_ext_size;
  1469. if (bfd_bwrite (debug->external_ext, amt, abfd) != amt)
  1470. goto error_return;
  1471. if (space != NULL)
  1472. free (space);
  1473. return TRUE;
  1474. error_return:
  1475. if (space != NULL)
  1476. free (space);
  1477. return FALSE;
  1478. }
  1479. /* Handle the find_nearest_line function for both ECOFF and MIPS ELF
  1480. files. */
  1481. /* Compare FDR entries. This is called via qsort. */
  1482. static int
  1483. cmp_fdrtab_entry (const void * leftp, const void * rightp)
  1484. {
  1485. const struct ecoff_fdrtab_entry *lp =
  1486. (const struct ecoff_fdrtab_entry *) leftp;
  1487. const struct ecoff_fdrtab_entry *rp =
  1488. (const struct ecoff_fdrtab_entry *) rightp;
  1489. if (lp->base_addr < rp->base_addr)
  1490. return -1;
  1491. if (lp->base_addr > rp->base_addr)
  1492. return 1;
  1493. return 0;
  1494. }
  1495. /* Each file descriptor (FDR) has a memory address, to simplify
  1496. looking up an FDR by address, we build a table covering all FDRs
  1497. that have a least one procedure descriptor in them. The final
  1498. table will be sorted by address so we can look it up via binary
  1499. search. */
  1500. static bfd_boolean
  1501. mk_fdrtab (bfd *abfd,
  1502. struct ecoff_debug_info * const debug_info,
  1503. const struct ecoff_debug_swap * const debug_swap,
  1504. struct ecoff_find_line *line_info)
  1505. {
  1506. struct ecoff_fdrtab_entry *tab;
  1507. FDR *fdr_ptr;
  1508. FDR *fdr_start;
  1509. FDR *fdr_end;
  1510. bfd_boolean stabs;
  1511. long len;
  1512. bfd_size_type amt;
  1513. fdr_start = debug_info->fdr;
  1514. fdr_end = fdr_start + debug_info->symbolic_header.ifdMax;
  1515. /* First, let's see how long the table needs to be. */
  1516. for (len = 0, fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
  1517. {
  1518. if (fdr_ptr->cpd == 0) /* Skip FDRs that have no PDRs. */
  1519. continue;
  1520. ++len;
  1521. }
  1522. /* Now, create and fill in the table. */
  1523. amt = (bfd_size_type) len * sizeof (struct ecoff_fdrtab_entry);
  1524. line_info->fdrtab = (struct ecoff_fdrtab_entry*) bfd_zalloc (abfd, amt);
  1525. if (line_info->fdrtab == NULL)
  1526. return FALSE;
  1527. line_info->fdrtab_len = len;
  1528. tab = line_info->fdrtab;
  1529. for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
  1530. {
  1531. if (fdr_ptr->cpd == 0)
  1532. continue;
  1533. /* Check whether this file has stabs debugging information. In
  1534. a file with stabs debugging information, the second local
  1535. symbol is named @stabs. */
  1536. stabs = FALSE;
  1537. if (fdr_ptr->csym >= 2)
  1538. {
  1539. char *sym_ptr;
  1540. SYMR sym;
  1541. sym_ptr = ((char *) debug_info->external_sym
  1542. + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
  1543. (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
  1544. if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
  1545. STABS_SYMBOL) == 0)
  1546. stabs = TRUE;
  1547. }
  1548. if (!stabs)
  1549. {
  1550. /* eraxxon: There are at least two problems with this computation:
  1551. 1) PDRs do *not* contain offsets but full vma's; and typically the
  1552. address of the first PDR is the address of the FDR, which will
  1553. make (most) of the results of the original computation 0!
  1554. 2) Once in a wacky while, the Compaq compiler generated PDR
  1555. addresses do not equal the FDR vma, but they (the PDR address)
  1556. are still vma's and not offsets. Cf. comments in
  1557. 'lookup_line'. */
  1558. /* The address of the first PDR is the offset of that
  1559. procedure relative to the beginning of file FDR. */
  1560. tab->base_addr = fdr_ptr->adr;
  1561. }
  1562. else
  1563. {
  1564. /* XXX I don't know about stabs, so this is a guess
  1565. (davidm@cs.arizona.edu). */
  1566. tab->base_addr = fdr_ptr->adr;
  1567. }
  1568. tab->fdr = fdr_ptr;
  1569. ++tab;
  1570. }
  1571. /* Finally, the table is sorted in increasing memory-address order.
  1572. The table is mostly sorted already, but there are cases (e.g.,
  1573. static functions in include files), where this does not hold.
  1574. Use "odump -PFv" to verify... */
  1575. qsort (line_info->fdrtab, (size_t) len,
  1576. sizeof (struct ecoff_fdrtab_entry), cmp_fdrtab_entry);
  1577. return TRUE;
  1578. }
  1579. /* Return index of first FDR that covers to OFFSET. */
  1580. static long
  1581. fdrtab_lookup (struct ecoff_find_line *line_info, bfd_vma offset)
  1582. {
  1583. long low, high, len;
  1584. long mid = -1;
  1585. struct ecoff_fdrtab_entry *tab;
  1586. len = line_info->fdrtab_len;
  1587. if (len == 0)
  1588. return -1;
  1589. tab = line_info->fdrtab;
  1590. for (low = 0, high = len - 1 ; low != high ;)
  1591. {
  1592. mid = (high + low) / 2;
  1593. if (offset >= tab[mid].base_addr && offset < tab[mid + 1].base_addr)
  1594. goto find_min;
  1595. if (tab[mid].base_addr > offset)
  1596. high = mid;
  1597. else
  1598. low = mid + 1;
  1599. }
  1600. /* eraxxon: at this point 'offset' is either lower than the lowest entry or
  1601. higher than the highest entry. In the former case high = low = mid = 0;
  1602. we want to return -1. In the latter case, low = high and mid = low - 1;
  1603. we want to return the index of the highest entry. Only in former case
  1604. will the following 'catch-all' test be true. */
  1605. ++mid;
  1606. /* Last entry is catch-all for all higher addresses. */
  1607. if (offset < tab[mid].base_addr)
  1608. return -1;
  1609. find_min:
  1610. /* eraxxon: There may be multiple FDRs in the table with the
  1611. same base_addr; make sure that we are at the first one. */
  1612. while (mid > 0 && tab[mid - 1].base_addr == tab[mid].base_addr)
  1613. --mid;
  1614. return mid;
  1615. }
  1616. /* Look up a line given an address, storing the information in
  1617. LINE_INFO->cache. */
  1618. static bfd_boolean
  1619. lookup_line (bfd *abfd,
  1620. struct ecoff_debug_info * const debug_info,
  1621. const struct ecoff_debug_swap * const debug_swap,
  1622. struct ecoff_find_line *line_info)
  1623. {
  1624. struct ecoff_fdrtab_entry *tab;
  1625. bfd_vma offset;
  1626. bfd_boolean stabs;
  1627. FDR *fdr_ptr;
  1628. int i;
  1629. /* eraxxon: note that 'offset' is the full vma, not a section offset. */
  1630. offset = line_info->cache.start;
  1631. /* Build FDR table (sorted by object file's base-address) if we
  1632. don't have it already. */
  1633. if (line_info->fdrtab == NULL
  1634. && !mk_fdrtab (abfd, debug_info, debug_swap, line_info))
  1635. return FALSE;
  1636. tab = line_info->fdrtab;
  1637. /* Find first FDR for address OFFSET. */
  1638. i = fdrtab_lookup (line_info, offset);
  1639. if (i < 0)
  1640. return FALSE; /* no FDR, no fun... */
  1641. /* eraxxon: 'fdrtab_lookup' doesn't give what we want, at least for Compaq's
  1642. C++ compiler 6.2. Consider three FDRs with starting addresses of x, y,
  1643. and z, respectively, such that x < y < z. Assume further that
  1644. y < 'offset' < z. It is possible at times that the PDR for 'offset' is
  1645. associated with FDR x and *not* with FDR y. Erg!!
  1646. From a binary dump of my C++ test case 'moo' using Compaq's coffobjanl
  1647. (output format has been edited for our purposes):
  1648. FDR [2]: (main.C): First instruction: 0x12000207c <x>
  1649. PDR [5] for File [2]: LoopTest__Xv <0x1200020a0> (a)
  1650. PDR [7] for File [2]: foo__Xv <0x120002168>
  1651. FDR [1]: (-1): First instruction: 0x1200020e8 <y>
  1652. PDR [3] for File [1]: <0x120001ad0> (b)
  1653. FDR [6]: (-1): First instruction: 0x1200026f0 <z>
  1654. (a) In the case of PDR5, the vma is such that the first few instructions
  1655. of the procedure can be found. But since the size of this procedure is
  1656. 160b, the vma will soon cross into the 'address space' of FDR1 and no
  1657. debugging info will be found. How repugnant!
  1658. (b) It is also possible for a PDR to have a *lower* vma than its associated
  1659. FDR; see FDR1 and PDR3. Gross!
  1660. Since the FDRs that are causing so much havok (in this case) 1) do not
  1661. describe actual files (fdr.rss == -1), and 2) contain only compiler
  1662. generated routines, I thought a simple fix would be to exclude them from
  1663. the FDR table in 'mk_fdrtab'. But, besides not knowing for certain
  1664. whether this would be correct, it creates an additional problem. If we
  1665. happen to ask for source file info on a compiler generated (procedure)
  1666. symbol -- which is still in the symbol table -- the result can be
  1667. information from a real procedure! This is because compiler generated
  1668. procedures with vma's higher than the last FDR in the fdr table will be
  1669. associated with a PDR from this FDR, specifically the PDR with the
  1670. highest vma. This wasn't a problem before, because each procedure had a
  1671. PDR. (Yes, this problem could be eliminated if we kept the size of the
  1672. last PDR around, but things are already getting ugly).
  1673. Probably, a better solution would be to have a sorted PDR table. Each
  1674. PDR would have a pointer to its FDR so file information could still be
  1675. obtained. A FDR table could still be constructed if necessary -- since
  1676. it only contains pointers, not much extra memory would be used -- but
  1677. the PDR table would be searched to locate debugging info.
  1678. There is still at least one remaining issue. Sometimes a FDR can have a
  1679. bogus name, but contain PDRs that should belong to another FDR with a
  1680. real name. E.g:
  1681. FDR [3]: 0000000120001b50 (/home/.../Array.H~alt~deccxx_5E5A62AD)
  1682. PDR [a] for File [3]: 0000000120001b50
  1683. PDR [b] for File [3]: 0000000120001cf0
  1684. PDR [c] for File [3]: 0000000120001dc8
  1685. PDR [d] for File [3]: 0000000120001e40
  1686. PDR [e] for File [3]: 0000000120001eb8
  1687. PDR [f] for File [3]: 0000000120001f4c
  1688. FDR [4]: 0000000120001b50 (/home/.../Array.H)
  1689. Here, FDR4 has the correct name, but should (seemingly) contain PDRa-f.
  1690. The symbol table for PDR4 does contain symbols for PDRa-f, but so does
  1691. the symbol table for FDR3. However the former is different; perhaps this
  1692. can be detected easily. (I'm not sure at this point.) This problem only
  1693. seems to be associated with files with templates. I am assuming the idea
  1694. is that there is a 'fake' FDR (with PDRs) for each differently typed set
  1695. of templates that must be generated. Currently, FDR4 is completely
  1696. excluded from the FDR table in 'mk_fdrtab' because it contains no PDRs.
  1697. Since I don't have time to prepare a real fix for this right now, be
  1698. prepared for 'A Horrible Hack' to force the inspection of all non-stabs
  1699. FDRs. It's coming... */
  1700. fdr_ptr = tab[i].fdr;
  1701. /* Check whether this file has stabs debugging information. In a
  1702. file with stabs debugging information, the second local symbol is
  1703. named @stabs. */
  1704. stabs = FALSE;
  1705. if (fdr_ptr->csym >= 2)
  1706. {
  1707. char *sym_ptr;
  1708. SYMR sym;
  1709. sym_ptr = ((char *) debug_info->external_sym
  1710. + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
  1711. (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
  1712. if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
  1713. STABS_SYMBOL) == 0)
  1714. stabs = TRUE;
  1715. }
  1716. if (!stabs)
  1717. {
  1718. bfd_size_type external_pdr_size;
  1719. char *pdr_ptr;
  1720. char *best_pdr = NULL;
  1721. FDR *best_fdr;
  1722. bfd_signed_vma best_dist = -1;
  1723. PDR pdr;
  1724. unsigned char *line_ptr;
  1725. unsigned char *line_end;
  1726. int lineno;
  1727. /* This file uses ECOFF debugging information. Each FDR has a
  1728. list of procedure descriptors (PDR). The address in the FDR
  1729. is the absolute address of the first procedure. The address
  1730. in the first PDR gives the offset of that procedure relative
  1731. to the object file's base-address. The addresses in
  1732. subsequent PDRs specify each procedure's address relative to
  1733. the object file's base-address. To make things more juicy,
  1734. whenever the PROF bit in the PDR is set, the real entry point
  1735. of the procedure may be 16 bytes below what would normally be
  1736. the procedure's entry point. Instead, DEC came up with a
  1737. wicked scheme to create profiled libraries "on the fly":
  1738. instead of shipping a regular and a profiled version of each
  1739. library, they insert 16 bytes of unused space in front of
  1740. each procedure and set the "prof" bit in the PDR to indicate
  1741. that there is a gap there (this is done automagically by "as"
  1742. when option "-pg" is specified). Thus, normally, you link
  1743. against such a library and, except for lots of 16 byte gaps
  1744. between functions, things will behave as usual. However,
  1745. when invoking "ld" with option "-pg", it will fill those gaps
  1746. with code that calls mcount(). It then moves the function's
  1747. entry point down by 16 bytes, and out pops a binary that has
  1748. all functions profiled.
  1749. NOTE: Neither FDRs nor PDRs are strictly sorted in memory
  1750. order. For example, when including header-files that
  1751. define functions, the FDRs follow behind the including
  1752. file, even though their code may have been generated at
  1753. a lower address. File coff-alpha.c from libbfd
  1754. illustrates this (use "odump -PFv" to look at a file's
  1755. FDR/PDR). Similarly, PDRs are sometimes out of order
  1756. as well. An example of this is OSF/1 v3.0 libc's
  1757. malloc.c. I'm not sure why this happens, but it could
  1758. be due to optimizations that reorder a function's
  1759. position within an object-file.
  1760. Strategy:
  1761. On the first call to this function, we build a table of FDRs
  1762. that is sorted by the base-address of the object-file the FDR
  1763. is referring to. Notice that each object-file may contain
  1764. code from multiple source files (e.g., due to code defined in
  1765. include files). Thus, for any given base-address, there may
  1766. be multiple FDRs (but this case is, fortunately, uncommon).
  1767. lookup(addr) guarantees to return the first FDR that applies
  1768. to address ADDR. Thus, after invoking lookup(), we have a
  1769. list of FDRs that may contain the PDR for ADDR. Next, we
  1770. walk through the PDRs of these FDRs and locate the one that
  1771. is closest to ADDR (i.e., for which the difference between
  1772. ADDR and the PDR's entry point is positive and minimal).
  1773. Once, the right FDR and PDR are located, we simply walk
  1774. through the line-number table to lookup the line-number that
  1775. best matches ADDR. Obviously, things could be sped up by
  1776. keeping a sorted list of PDRs instead of a sorted list of
  1777. FDRs. However, this would increase space requirements
  1778. considerably, which is undesirable. */
  1779. external_pdr_size = debug_swap->external_pdr_size;
  1780. /* eraxxon: The Horrible Hack: Because of the problems above, set 'i'
  1781. to 0 so we look through all FDRs.
  1782. Because FDR's without any symbols are assumed to be non-stabs,
  1783. searching through all FDRs may cause the following code to try to
  1784. read stabs FDRs as ECOFF ones. However, I don't think this will
  1785. harm anything. */
  1786. i = 0;
  1787. /* Search FDR list starting at tab[i] for the PDR that best matches
  1788. OFFSET. Normally, the FDR list is only one entry long. */
  1789. best_fdr = NULL;
  1790. do
  1791. {
  1792. /* eraxxon: 'dist' and 'min_dist' can be negative now
  1793. because we iterate over every FDR rather than just ones
  1794. with a base address less than or equal to 'offset'. */
  1795. bfd_signed_vma dist = -1, min_dist = -1;
  1796. char *pdr_hold;
  1797. char *pdr_end;
  1798. fdr_ptr = tab[i].fdr;
  1799. pdr_ptr = ((char *) debug_info->external_pdr
  1800. + fdr_ptr->ipdFirst * external_pdr_size);
  1801. pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
  1802. (*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr);
  1803. /* Find PDR that is closest to OFFSET. If pdr.prof is set,
  1804. the procedure entry-point *may* be 0x10 below pdr.adr. We
  1805. simply pretend that pdr.prof *implies* a lower entry-point.
  1806. This is safe because it just means that may identify 4 NOPs
  1807. in front of the function as belonging to the function. */
  1808. for (pdr_hold = NULL;
  1809. pdr_ptr < pdr_end;
  1810. (pdr_ptr += external_pdr_size,
  1811. (*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr)))
  1812. {
  1813. if (offset >= (pdr.adr - 0x10 * pdr.prof))
  1814. {
  1815. dist = offset - (pdr.adr - 0x10 * pdr.prof);
  1816. /* eraxxon: 'dist' can be negative now. Note that
  1817. 'min_dist' can be negative if 'pdr_hold' below is NULL. */
  1818. if (!pdr_hold || (dist >= 0 && dist < min_dist))
  1819. {
  1820. min_dist = dist;
  1821. pdr_hold = pdr_ptr;
  1822. }
  1823. }
  1824. }
  1825. if (!best_pdr || (min_dist >= 0 && min_dist < best_dist))
  1826. {
  1827. best_dist = (bfd_vma) min_dist;
  1828. best_fdr = fdr_ptr;
  1829. best_pdr = pdr_hold;
  1830. }
  1831. /* Continue looping until base_addr of next entry is different. */
  1832. }
  1833. /* eraxxon: We want to iterate over all FDRs.
  1834. See previous comment about 'fdrtab_lookup'. */
  1835. while (++i < line_info->fdrtab_len);
  1836. if (!best_fdr || !best_pdr)
  1837. return FALSE; /* Shouldn't happen... */
  1838. /* Phew, finally we got something that we can hold onto. */
  1839. fdr_ptr = best_fdr;
  1840. pdr_ptr = best_pdr;
  1841. (*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr);
  1842. /* Now we can look for the actual line number. The line numbers
  1843. are stored in a very funky format, which I won't try to
  1844. describe. The search is bounded by the end of the FDRs line
  1845. number entries. */
  1846. line_end = debug_info->line + fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
  1847. /* Make offset relative to procedure entry. */
  1848. offset -= pdr.adr - 0x10 * pdr.prof;
  1849. lineno = pdr.lnLow;
  1850. line_ptr = debug_info->line + fdr_ptr->cbLineOffset + pdr.cbLineOffset;
  1851. while (line_ptr < line_end)
  1852. {
  1853. int delta;
  1854. unsigned int count;
  1855. delta = *line_ptr >> 4;
  1856. if (delta >= 0x8)
  1857. delta -= 0x10;
  1858. count = (*line_ptr & 0xf) + 1;
  1859. ++line_ptr;
  1860. if (delta == -8)
  1861. {
  1862. delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
  1863. if (delta >= 0x8000)
  1864. delta -= 0x10000;
  1865. line_ptr += 2;
  1866. }
  1867. lineno += delta;
  1868. if (offset < count * 4)
  1869. {
  1870. line_info->cache.stop += count * 4 - offset;
  1871. break;
  1872. }
  1873. offset -= count * 4;
  1874. }
  1875. /* If fdr_ptr->rss is -1, then this file does not have full
  1876. symbols, at least according to gdb/mipsread.c. */
  1877. if (fdr_ptr->rss == -1)
  1878. {
  1879. line_info->cache.filename = NULL;
  1880. if (pdr.isym == -1)
  1881. line_info->cache.functionname = NULL;
  1882. else
  1883. {
  1884. EXTR proc_ext;
  1885. (*debug_swap->swap_ext_in)
  1886. (abfd,
  1887. ((char *) debug_info->external_ext
  1888. + pdr.isym * debug_swap->external_ext_size),
  1889. &proc_ext);
  1890. line_info->cache.functionname = (debug_info->ssext
  1891. + proc_ext.asym.iss);
  1892. }
  1893. }
  1894. else
  1895. {
  1896. SYMR proc_sym;
  1897. line_info->cache.filename = (debug_info->ss
  1898. + fdr_ptr->issBase
  1899. + fdr_ptr->rss);
  1900. (*debug_swap->swap_sym_in)
  1901. (abfd,
  1902. ((char *) debug_info->external_sym
  1903. + ((fdr_ptr->isymBase + pdr.isym)
  1904. * debug_swap->external_sym_size)),
  1905. &proc_sym);
  1906. line_info->cache.functionname = (debug_info->ss
  1907. + fdr_ptr->issBase
  1908. + proc_sym.iss);
  1909. }
  1910. if (lineno == ilineNil)
  1911. lineno = 0;
  1912. line_info->cache.line_num = lineno;
  1913. }
  1914. else
  1915. {
  1916. bfd_size_type external_sym_size;
  1917. const char *directory_name;
  1918. const char *main_file_name;
  1919. const char *current_file_name;
  1920. const char *function_name;
  1921. const char *line_file_name;
  1922. bfd_vma low_func_vma;
  1923. bfd_vma low_line_vma;
  1924. bfd_boolean past_line;
  1925. bfd_boolean past_fn;
  1926. char *sym_ptr, *sym_ptr_end;
  1927. size_t len, funclen;
  1928. char *buffer = NULL;
  1929. /* This file uses stabs debugging information. When gcc is not
  1930. optimizing, it will put the line number information before
  1931. the function name stabs entry. When gcc is optimizing, it
  1932. will put the stabs entry for all the function first, followed
  1933. by the line number information. (This appears to happen
  1934. because of the two output files used by the -mgpopt switch,
  1935. which is implied by -O). This means that we must keep
  1936. looking through the symbols until we find both a line number
  1937. and a function name which are beyond the address we want. */
  1938. line_info->cache.filename = NULL;
  1939. line_info->cache.functionname = NULL;
  1940. line_info->cache.line_num = 0;
  1941. directory_name = NULL;
  1942. main_file_name = NULL;
  1943. current_file_name = NULL;
  1944. function_name = NULL;
  1945. line_file_name = NULL;
  1946. low_func_vma = 0;
  1947. low_line_vma = 0;
  1948. past_line = FALSE;
  1949. past_fn = FALSE;
  1950. external_sym_size = debug_swap->external_sym_size;
  1951. sym_ptr = ((char *) debug_info->external_sym
  1952. + (fdr_ptr->isymBase + 2) * external_sym_size);
  1953. sym_ptr_end = sym_ptr + (fdr_ptr->csym - 2) * external_sym_size;
  1954. for (;
  1955. sym_ptr < sym_ptr_end && (! past_line || ! past_fn);
  1956. sym_ptr += external_sym_size)
  1957. {
  1958. SYMR sym;
  1959. (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
  1960. if (ECOFF_IS_STAB (&sym))
  1961. {
  1962. switch (ECOFF_UNMARK_STAB (sym.index))
  1963. {
  1964. case N_SO:
  1965. main_file_name = current_file_name =
  1966. debug_info->ss + fdr_ptr->issBase + sym.iss;
  1967. /* Check the next symbol to see if it is also an
  1968. N_SO symbol. */
  1969. if (sym_ptr + external_sym_size < sym_ptr_end)
  1970. {
  1971. SYMR nextsym;
  1972. (*debug_swap->swap_sym_in) (abfd,
  1973. sym_ptr + external_sym_size,
  1974. &nextsym);
  1975. if (ECOFF_IS_STAB (&nextsym)
  1976. && ECOFF_UNMARK_STAB (nextsym.index) == N_SO)
  1977. {
  1978. directory_name = current_file_name;
  1979. main_file_name = current_file_name =
  1980. debug_info->ss + fdr_ptr->issBase + nextsym.iss;
  1981. sym_ptr += external_sym_size;
  1982. }
  1983. }
  1984. break;
  1985. case N_SOL:
  1986. current_file_name =
  1987. debug_info->ss + fdr_ptr->issBase + sym.iss;
  1988. break;
  1989. case N_FUN:
  1990. if (sym.value > offset)
  1991. past_fn = TRUE;
  1992. else if (sym.value >= low_func_vma)
  1993. {
  1994. low_func_vma = sym.value;
  1995. function_name =
  1996. debug_info->ss + fdr_ptr->issBase + sym.iss;
  1997. }
  1998. break;
  1999. }
  2000. }
  2001. else if (sym.st == stLabel && sym.index != indexNil)
  2002. {
  2003. if (sym.value > offset)
  2004. past_line = TRUE;
  2005. else if (sym.value >= low_line_vma)
  2006. {
  2007. low_line_vma = sym.value;
  2008. line_file_name = current_file_name;
  2009. line_info->cache.line_num = sym.index;
  2010. }
  2011. }
  2012. }
  2013. if (line_info->cache.line_num != 0)
  2014. main_file_name = line_file_name;
  2015. /* We need to remove the stuff after the colon in the function
  2016. name. We also need to put the directory name and the file
  2017. name together. */
  2018. if (function_name == NULL)
  2019. len = funclen = 0;
  2020. else
  2021. len = funclen = strlen (function_name) + 1;
  2022. if (main_file_name != NULL
  2023. && directory_name != NULL
  2024. && main_file_name[0] != '/')
  2025. len += strlen (directory_name) + strlen (main_file_name) + 1;
  2026. if (len != 0)
  2027. {
  2028. if (line_info->find_buffer != NULL)
  2029. free (line_info->find_buffer);
  2030. buffer = (char *) bfd_malloc ((bfd_size_type) len);
  2031. if (buffer == NULL)
  2032. return FALSE;
  2033. line_info->find_buffer = buffer;
  2034. }
  2035. if (function_name != NULL)
  2036. {
  2037. char *colon;
  2038. strcpy (buffer, function_name);
  2039. colon = strchr (buffer, ':');
  2040. if (colon != NULL)
  2041. *colon = '\0';
  2042. line_info->cache.functionname = buffer;
  2043. }
  2044. if (main_file_name != NULL)
  2045. {
  2046. if (directory_name == NULL || main_file_name[0] == '/')
  2047. line_info->cache.filename = main_file_name;
  2048. else
  2049. {
  2050. sprintf (buffer + funclen, "%s%s", directory_name,
  2051. main_file_name);
  2052. line_info->cache.filename = buffer + funclen;
  2053. }
  2054. }
  2055. }
  2056. return TRUE;
  2057. }
  2058. /* Do the work of find_nearest_line. */
  2059. bfd_boolean
  2060. _bfd_ecoff_locate_line (bfd *abfd,
  2061. asection *section,
  2062. bfd_vma offset,
  2063. struct ecoff_debug_info * const debug_info,
  2064. const struct ecoff_debug_swap * const debug_swap,
  2065. struct ecoff_find_line *line_info,
  2066. const char **filename_ptr,
  2067. const char **functionname_ptr,
  2068. unsigned int *retline_ptr)
  2069. {
  2070. offset += section->vma;
  2071. if (line_info->cache.sect == NULL
  2072. || line_info->cache.sect != section
  2073. || offset < line_info->cache.start
  2074. || offset >= line_info->cache.stop)
  2075. {
  2076. line_info->cache.sect = section;
  2077. line_info->cache.start = offset;
  2078. line_info->cache.stop = offset;
  2079. if (! lookup_line (abfd, debug_info, debug_swap, line_info))
  2080. {
  2081. line_info->cache.sect = NULL;
  2082. return FALSE;
  2083. }
  2084. }
  2085. *filename_ptr = line_info->cache.filename;
  2086. *functionname_ptr = line_info->cache.functionname;
  2087. *retline_ptr = line_info->cache.line_num;
  2088. return TRUE;
  2089. }
  2090. /* These routines copy symbolic information into a memory buffer.
  2091. FIXME: The whole point of the shuffle code is to avoid storing
  2092. everything in memory, since the linker is such a memory hog. This
  2093. code makes that effort useless. It is only called by the MIPS ELF
  2094. code when generating a shared library, so it is not that big a
  2095. deal, but it should be fixed eventually. */
  2096. /* Collect a shuffle into a memory buffer. */
  2097. static bfd_boolean
  2098. ecoff_collect_shuffle (struct shuffle *l, bfd_byte *buff)
  2099. {
  2100. unsigned long total;
  2101. total = 0;
  2102. for (; l != (struct shuffle *) NULL; l = l->next)
  2103. {
  2104. if (! l->filep)
  2105. memcpy (buff, l->u.memory, l->size);
  2106. else
  2107. {
  2108. if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
  2109. || (bfd_bread (buff, (bfd_size_type) l->size, l->u.file.input_bfd)
  2110. != l->size))
  2111. return FALSE;
  2112. }
  2113. total += l->size;
  2114. buff += l->size;
  2115. }
  2116. return TRUE;
  2117. }
  2118. /* Copy PDR information into a memory buffer. */
  2119. bfd_boolean
  2120. _bfd_ecoff_get_accumulated_pdr (void * handle,
  2121. bfd_byte *buff)
  2122. {
  2123. struct accumulate *ainfo = (struct accumulate *) handle;
  2124. return ecoff_collect_shuffle (ainfo->pdr, buff);
  2125. }
  2126. /* Copy symbol information into a memory buffer. */
  2127. bfd_boolean
  2128. _bfd_ecoff_get_accumulated_sym (void * handle, bfd_byte *buff)
  2129. {
  2130. struct accumulate *ainfo = (struct accumulate *) handle;
  2131. return ecoff_collect_shuffle (ainfo->sym, buff);
  2132. }
  2133. /* Copy the string table into a memory buffer. */
  2134. bfd_boolean
  2135. _bfd_ecoff_get_accumulated_ss (void * handle, bfd_byte *buff)
  2136. {
  2137. struct accumulate *ainfo = (struct accumulate *) handle;
  2138. struct string_hash_entry *sh;
  2139. unsigned long total;
  2140. /* The string table is written out from the hash table if this is a
  2141. final link. */
  2142. BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
  2143. *buff++ = '\0';
  2144. total = 1;
  2145. BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
  2146. for (sh = ainfo->ss_hash;
  2147. sh != (struct string_hash_entry *) NULL;
  2148. sh = sh->next)
  2149. {
  2150. size_t len;
  2151. len = strlen (sh->root.string);
  2152. memcpy (buff, sh->root.string, len + 1);
  2153. total += len + 1;
  2154. buff += len + 1;
  2155. }
  2156. return TRUE;
  2157. }