nlmcode.h 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. /* NLM (NetWare Loadable Module) executable support for BFD.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Written by Fred Fish @ Cygnus Support, using ELF support as the
  4. template.
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "libbfd.h"
  21. #include "libnlm.h"
  22. /* The functions in this file do not use the names they appear to use.
  23. This file is actually compiled multiple times, once for each size
  24. of NLM target we are using. At each size we use a different name,
  25. constructed by the macro nlmNAME. For example, the function which
  26. is named nlm_symbol_type below is actually named nlm32_symbol_type
  27. in the final executable. */
  28. #define Nlm_External_Fixed_Header NlmNAME (External_Fixed_Header)
  29. #define Nlm_External_Version_Header NlmNAME (External_Version_Header)
  30. #define Nlm_External_Copyright_Header NlmNAME (External_Copyright_Header)
  31. #define Nlm_External_Extended_Header NlmNAME (External_Extended_Header)
  32. #define Nlm_External_Custom_Header NlmNAME (External_Custom_Header)
  33. #define Nlm_External_Cygnus_Ext_Header NlmNAME (External_Cygnus_Ext_Header)
  34. #define nlm_symbol_type nlmNAME (symbol_type)
  35. #define nlm_get_symtab_upper_bound nlmNAME (get_symtab_upper_bound)
  36. #define nlm_canonicalize_symtab nlmNAME (canonicalize_symtab)
  37. #define nlm_make_empty_symbol nlmNAME (make_empty_symbol)
  38. #define nlm_print_symbol nlmNAME (print_symbol)
  39. #define nlm_get_symbol_info nlmNAME (get_symbol_info)
  40. #define nlm_get_reloc_upper_bound nlmNAME (get_reloc_upper_bound)
  41. #define nlm_canonicalize_reloc nlmNAME (canonicalize_reloc)
  42. #define nlm_object_p nlmNAME (object_p)
  43. #define nlm_set_section_contents nlmNAME (set_section_contents)
  44. #define nlm_write_object_contents nlmNAME (write_object_contents)
  45. #define nlm_swap_fixed_header_in(abfd,src,dst) \
  46. (nlm_swap_fixed_header_in_func (abfd)) (abfd, src, dst)
  47. #define nlm_swap_fixed_header_out(abfd,src,dst) \
  48. (nlm_swap_fixed_header_out_func (abfd)) (abfd, src, dst)
  49. /* Should perhaps use put_offset, put_word, etc. For now, the two versions
  50. can be handled by explicitly specifying 32 bits or "the long type". */
  51. #if ARCH_SIZE == 64
  52. #define put_word H_PUT_64
  53. #define get_word H_GET_64
  54. #endif
  55. #if ARCH_SIZE == 32
  56. #define put_word H_PUT_32
  57. #define get_word H_GET_32
  58. #endif
  59. /* Read and swap in the variable length header. All the fields must
  60. exist in the NLM, and must exist in the order they are read here. */
  61. static bfd_boolean
  62. nlm_swap_variable_header_in (bfd *abfd)
  63. {
  64. unsigned char temp[NLM_TARGET_LONG_SIZE];
  65. bfd_size_type amt;
  66. /* Read the description length and text members. */
  67. amt = sizeof (nlm_variable_header (abfd)->descriptionLength);
  68. if (bfd_bread ((void *) &nlm_variable_header (abfd)->descriptionLength,
  69. amt, abfd) != amt)
  70. return FALSE;
  71. amt = nlm_variable_header (abfd)->descriptionLength + 1;
  72. if (bfd_bread ((void *) nlm_variable_header (abfd)->descriptionText,
  73. amt, abfd) != amt)
  74. return FALSE;
  75. /* Read and convert the stackSize field. */
  76. amt = sizeof (temp);
  77. if (bfd_bread ((void *) temp, amt, abfd) != amt)
  78. return FALSE;
  79. nlm_variable_header (abfd)->stackSize = get_word (abfd, (bfd_byte *) temp);
  80. /* Read and convert the reserved field. */
  81. amt = sizeof (temp);
  82. if (bfd_bread ((void *) temp, amt, abfd) != amt)
  83. return FALSE;
  84. nlm_variable_header (abfd)->reserved = get_word (abfd, (bfd_byte *) temp);
  85. /* Read the oldThreadName field. This field is a fixed length string. */
  86. amt = sizeof (nlm_variable_header (abfd)->oldThreadName);
  87. if (bfd_bread ((void *) nlm_variable_header (abfd)->oldThreadName,
  88. amt, abfd) != amt)
  89. return FALSE;
  90. /* Read the screen name length and text members. */
  91. amt = sizeof (nlm_variable_header (abfd)->screenNameLength);
  92. if (bfd_bread ((void *) & nlm_variable_header (abfd)->screenNameLength,
  93. amt, abfd) != amt)
  94. return FALSE;
  95. amt = nlm_variable_header (abfd)->screenNameLength + 1;
  96. if (bfd_bread ((void *) nlm_variable_header (abfd)->screenName,
  97. amt, abfd) != amt)
  98. return FALSE;
  99. /* Read the thread name length and text members. */
  100. amt = sizeof (nlm_variable_header (abfd)->threadNameLength);
  101. if (bfd_bread ((void *) & nlm_variable_header (abfd)->threadNameLength,
  102. amt, abfd) != amt)
  103. return FALSE;
  104. amt = nlm_variable_header (abfd)->threadNameLength + 1;
  105. if (bfd_bread ((void *) nlm_variable_header (abfd)->threadName,
  106. amt, abfd) != amt)
  107. return FALSE;
  108. return TRUE;
  109. }
  110. /* Add a section to the bfd. */
  111. static bfd_boolean
  112. add_bfd_section (bfd *abfd,
  113. char *name,
  114. file_ptr offset,
  115. bfd_size_type size,
  116. flagword flags)
  117. {
  118. asection *newsect;
  119. newsect = bfd_make_section_with_flags (abfd, name, flags);
  120. if (newsect == NULL)
  121. return FALSE;
  122. newsect->vma = 0; /* NLM's are relocatable. */
  123. newsect->size = size;
  124. newsect->filepos = offset;
  125. newsect->alignment_power = bfd_log2 ((bfd_vma) 0); /* FIXME */
  126. return TRUE;
  127. }
  128. /* Read and swap in the contents of all the auxiliary headers. Because of
  129. the braindead design, we have to do strcmps on strings of indeterminate
  130. length to figure out what each auxiliary header is. Even worse, we have
  131. no way of knowing how many auxiliary headers there are or where the end
  132. of the auxiliary headers are, except by finding something that doesn't
  133. look like a known auxiliary header. This means that the first new type
  134. of auxiliary header added will break all existing tools that don't
  135. recognize it. */
  136. static bfd_boolean
  137. nlm_swap_auxiliary_headers_in (bfd *abfd)
  138. {
  139. char tempstr[16];
  140. file_ptr position;
  141. bfd_size_type amt;
  142. for (;;)
  143. {
  144. position = bfd_tell (abfd);
  145. amt = sizeof (tempstr);
  146. if (bfd_bread ((void *) tempstr, amt, abfd) != amt)
  147. return FALSE;
  148. if (bfd_seek (abfd, position, SEEK_SET) != 0)
  149. return FALSE;
  150. if (CONST_STRNEQ (tempstr, "VeRsIoN#"))
  151. {
  152. Nlm_External_Version_Header thdr;
  153. amt = sizeof (thdr);
  154. if (bfd_bread ((void *) &thdr, amt, abfd) != amt)
  155. return FALSE;
  156. memcpy (nlm_version_header (abfd)->stamp, thdr.stamp,
  157. sizeof (thdr.stamp));
  158. nlm_version_header (abfd)->majorVersion =
  159. get_word (abfd, (bfd_byte *) thdr.majorVersion);
  160. nlm_version_header (abfd)->minorVersion =
  161. get_word (abfd, (bfd_byte *) thdr.minorVersion);
  162. nlm_version_header (abfd)->revision =
  163. get_word (abfd, (bfd_byte *) thdr.revision);
  164. nlm_version_header (abfd)->year =
  165. get_word (abfd, (bfd_byte *) thdr.year);
  166. nlm_version_header (abfd)->month =
  167. get_word (abfd, (bfd_byte *) thdr.month);
  168. nlm_version_header (abfd)->day =
  169. get_word (abfd, (bfd_byte *) thdr.day);
  170. }
  171. else if (CONST_STRNEQ (tempstr, "MeSsAgEs"))
  172. {
  173. Nlm_External_Extended_Header thdr;
  174. amt = sizeof (thdr);
  175. if (bfd_bread ((void *) &thdr, amt, abfd) != amt)
  176. return FALSE;
  177. memcpy (nlm_extended_header (abfd)->stamp, thdr.stamp,
  178. sizeof (thdr.stamp));
  179. nlm_extended_header (abfd)->languageID =
  180. get_word (abfd, (bfd_byte *) thdr.languageID);
  181. nlm_extended_header (abfd)->messageFileOffset =
  182. get_word (abfd, (bfd_byte *) thdr.messageFileOffset);
  183. nlm_extended_header (abfd)->messageFileLength =
  184. get_word (abfd, (bfd_byte *) thdr.messageFileLength);
  185. nlm_extended_header (abfd)->messageCount =
  186. get_word (abfd, (bfd_byte *) thdr.messageCount);
  187. nlm_extended_header (abfd)->helpFileOffset =
  188. get_word (abfd, (bfd_byte *) thdr.helpFileOffset);
  189. nlm_extended_header (abfd)->helpFileLength =
  190. get_word (abfd, (bfd_byte *) thdr.helpFileLength);
  191. nlm_extended_header (abfd)->RPCDataOffset =
  192. get_word (abfd, (bfd_byte *) thdr.RPCDataOffset);
  193. nlm_extended_header (abfd)->RPCDataLength =
  194. get_word (abfd, (bfd_byte *) thdr.RPCDataLength);
  195. nlm_extended_header (abfd)->sharedCodeOffset =
  196. get_word (abfd, (bfd_byte *) thdr.sharedCodeOffset);
  197. nlm_extended_header (abfd)->sharedCodeLength =
  198. get_word (abfd, (bfd_byte *) thdr.sharedCodeLength);
  199. nlm_extended_header (abfd)->sharedDataOffset =
  200. get_word (abfd, (bfd_byte *) thdr.sharedDataOffset);
  201. nlm_extended_header (abfd)->sharedDataLength =
  202. get_word (abfd, (bfd_byte *) thdr.sharedDataLength);
  203. nlm_extended_header (abfd)->sharedRelocationFixupOffset =
  204. get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupOffset);
  205. nlm_extended_header (abfd)->sharedRelocationFixupCount =
  206. get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupCount);
  207. nlm_extended_header (abfd)->sharedExternalReferenceOffset =
  208. get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceOffset);
  209. nlm_extended_header (abfd)->sharedExternalReferenceCount =
  210. get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceCount);
  211. nlm_extended_header (abfd)->sharedPublicsOffset =
  212. get_word (abfd, (bfd_byte *) thdr.sharedPublicsOffset);
  213. nlm_extended_header (abfd)->sharedPublicsCount =
  214. get_word (abfd, (bfd_byte *) thdr.sharedPublicsCount);
  215. nlm_extended_header (abfd)->sharedDebugRecordOffset =
  216. get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordOffset);
  217. nlm_extended_header (abfd)->sharedDebugRecordCount =
  218. get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordCount);
  219. nlm_extended_header (abfd)->SharedInitializationOffset =
  220. get_word (abfd, (bfd_byte *) thdr.sharedInitializationOffset);
  221. nlm_extended_header (abfd)->SharedExitProcedureOffset =
  222. get_word (abfd, (bfd_byte *) thdr.SharedExitProcedureOffset);
  223. nlm_extended_header (abfd)->productID =
  224. get_word (abfd, (bfd_byte *) thdr.productID);
  225. nlm_extended_header (abfd)->reserved0 =
  226. get_word (abfd, (bfd_byte *) thdr.reserved0);
  227. nlm_extended_header (abfd)->reserved1 =
  228. get_word (abfd, (bfd_byte *) thdr.reserved1);
  229. nlm_extended_header (abfd)->reserved2 =
  230. get_word (abfd, (bfd_byte *) thdr.reserved2);
  231. nlm_extended_header (abfd)->reserved3 =
  232. get_word (abfd, (bfd_byte *) thdr.reserved3);
  233. nlm_extended_header (abfd)->reserved4 =
  234. get_word (abfd, (bfd_byte *) thdr.reserved4);
  235. nlm_extended_header (abfd)->reserved5 =
  236. get_word (abfd, (bfd_byte *) thdr.reserved5);
  237. }
  238. else if (CONST_STRNEQ (tempstr, "CoPyRiGhT="))
  239. {
  240. amt = sizeof (nlm_copyright_header (abfd)->stamp);
  241. if (bfd_bread ((void *) nlm_copyright_header (abfd)->stamp,
  242. amt, abfd) != amt)
  243. return FALSE;
  244. if (bfd_bread ((void *) &(nlm_copyright_header (abfd)
  245. ->copyrightMessageLength),
  246. (bfd_size_type) 1, abfd) != 1)
  247. return FALSE;
  248. /* The copyright message is a variable length string. */
  249. amt = nlm_copyright_header (abfd)->copyrightMessageLength + 1;
  250. if (bfd_bread ((void *) nlm_copyright_header (abfd)->copyrightMessage,
  251. amt, abfd) != amt)
  252. return FALSE;
  253. }
  254. else if (CONST_STRNEQ (tempstr, "CuStHeAd"))
  255. {
  256. Nlm_External_Custom_Header thdr;
  257. bfd_size_type hdrLength;
  258. file_ptr dataOffset;
  259. bfd_size_type dataLength;
  260. char dataStamp[8];
  261. void * hdr;
  262. /* Read the stamp ("CuStHeAd"). */
  263. amt = sizeof (thdr.stamp);
  264. if (bfd_bread ((void *) thdr.stamp, amt, abfd) != amt)
  265. return FALSE;
  266. /* Read the length of this custom header. */
  267. amt = sizeof (thdr.length);
  268. if (bfd_bread ((void *) thdr.length, amt, abfd) != amt)
  269. return FALSE;
  270. hdrLength = get_word (abfd, (bfd_byte *) thdr.length);
  271. /* Read further fields if we have them. */
  272. if (hdrLength < NLM_TARGET_LONG_SIZE)
  273. dataOffset = 0;
  274. else
  275. {
  276. amt = sizeof (thdr.dataOffset);
  277. if (bfd_bread ((void *) thdr.dataOffset, amt, abfd) != amt)
  278. return FALSE;
  279. dataOffset = get_word (abfd, (bfd_byte *) thdr.dataOffset);
  280. }
  281. if (hdrLength < 2 * NLM_TARGET_LONG_SIZE)
  282. dataLength = 0;
  283. else
  284. {
  285. amt = sizeof (thdr.dataLength);
  286. if (bfd_bread ((void *) thdr.dataLength, amt, abfd) != amt)
  287. return FALSE;
  288. dataLength = get_word (abfd, (bfd_byte *) thdr.dataLength);
  289. }
  290. if (hdrLength < 2 * NLM_TARGET_LONG_SIZE + 8)
  291. memset (dataStamp, 0, sizeof (dataStamp));
  292. else
  293. {
  294. amt = sizeof (dataStamp);
  295. if (bfd_bread ((void *) dataStamp, amt, abfd) != amt)
  296. return FALSE;
  297. }
  298. /* Read the rest of the header, if any. */
  299. if (hdrLength <= 2 * NLM_TARGET_LONG_SIZE + 8)
  300. {
  301. hdr = NULL;
  302. hdrLength = 0;
  303. }
  304. else
  305. {
  306. hdrLength -= 2 * NLM_TARGET_LONG_SIZE + 8;
  307. hdr = bfd_alloc (abfd, hdrLength);
  308. if (hdr == NULL)
  309. return FALSE;
  310. if (bfd_bread (hdr, hdrLength, abfd) != hdrLength)
  311. return FALSE;
  312. }
  313. /* If we have found a Cygnus header, process it. Otherwise,
  314. just save the associated data without trying to interpret
  315. it. */
  316. if (CONST_STRNEQ (dataStamp, "CyGnUsEx"))
  317. {
  318. file_ptr pos;
  319. bfd_byte *contents;
  320. bfd_byte *p, *pend;
  321. BFD_ASSERT (hdrLength == 0 && hdr == NULL);
  322. pos = bfd_tell (abfd);
  323. if (bfd_seek (abfd, dataOffset, SEEK_SET) != 0)
  324. return FALSE;
  325. contents = bfd_alloc (abfd, dataLength);
  326. if (contents == NULL)
  327. return FALSE;
  328. if (bfd_bread (contents, dataLength, abfd) != dataLength)
  329. return FALSE;
  330. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  331. return FALSE;
  332. LITMEMCPY (nlm_cygnus_ext_header (abfd), "CyGnUsEx");
  333. nlm_cygnus_ext_header (abfd)->offset = dataOffset;
  334. nlm_cygnus_ext_header (abfd)->length = dataLength;
  335. /* This data this header points to provides a list of
  336. the sections which were in the original object file
  337. which was converted to become an NLM. We locate
  338. those sections and add them to the BFD. Note that
  339. this is likely to create a second .text, .data and
  340. .bss section; retrieving the sections by name will
  341. get the actual NLM sections, which is what we want to
  342. happen. The sections from the original file, which
  343. may be subsets of the NLM section, can only be found
  344. using bfd_map_over_sections. */
  345. p = contents;
  346. pend = p + dataLength;
  347. while (p < pend)
  348. {
  349. char *name;
  350. size_t l;
  351. file_ptr filepos;
  352. bfd_size_type size;
  353. asection *newsec;
  354. /* The format of this information is
  355. null terminated section name
  356. zeroes to adjust to 4 byte boundary
  357. 4 byte section data file pointer
  358. 4 byte section size. */
  359. name = (char *) p;
  360. l = strlen (name) + 1;
  361. l = (l + 3) &~ (size_t) 3;
  362. p += l;
  363. filepos = H_GET_32 (abfd, p);
  364. p += 4;
  365. size = H_GET_32 (abfd, p);
  366. p += 4;
  367. newsec = bfd_make_section_anyway (abfd, name);
  368. if (newsec == NULL)
  369. return FALSE;
  370. newsec->size = size;
  371. if (filepos != 0)
  372. {
  373. newsec->filepos = filepos;
  374. newsec->flags |= SEC_HAS_CONTENTS;
  375. }
  376. }
  377. }
  378. else
  379. {
  380. memcpy (nlm_custom_header (abfd)->stamp, thdr.stamp,
  381. sizeof (thdr.stamp));
  382. nlm_custom_header (abfd)->hdrLength = hdrLength;
  383. nlm_custom_header (abfd)->dataOffset = dataOffset;
  384. nlm_custom_header (abfd)->dataLength = dataLength;
  385. memcpy (nlm_custom_header (abfd)->dataStamp, dataStamp,
  386. sizeof (dataStamp));
  387. nlm_custom_header (abfd)->hdr = hdr;
  388. }
  389. }
  390. else
  391. break;
  392. }
  393. return TRUE;
  394. }
  395. const bfd_target *
  396. nlm_object_p (bfd *abfd)
  397. {
  398. struct nlm_obj_tdata *preserved_tdata = nlm_tdata (abfd);
  399. bfd_boolean (*backend_object_p) (bfd *);
  400. void * x_fxdhdr = NULL;
  401. Nlm_Internal_Fixed_Header *i_fxdhdrp;
  402. struct nlm_obj_tdata *new_tdata = NULL;
  403. const char *signature;
  404. enum bfd_architecture arch;
  405. bfd_size_type amt;
  406. /* Some NLM formats have a prefix before the standard NLM fixed
  407. header. */
  408. backend_object_p = nlm_backend_object_p_func (abfd);
  409. if (backend_object_p)
  410. {
  411. if (!(*backend_object_p) (abfd))
  412. goto got_wrong_format_error;
  413. }
  414. /* Read in the fixed length portion of the NLM header in external format. */
  415. amt = nlm_fixed_header_size (abfd);
  416. x_fxdhdr = bfd_malloc (amt);
  417. if (x_fxdhdr == NULL)
  418. goto got_no_match;
  419. if (bfd_bread ((void *) x_fxdhdr, amt, abfd) != amt)
  420. {
  421. if (bfd_get_error () != bfd_error_system_call)
  422. goto got_wrong_format_error;
  423. else
  424. goto got_no_match;
  425. }
  426. /* Allocate an instance of the nlm_obj_tdata structure and hook it up to
  427. the tdata pointer in the bfd. */
  428. amt = sizeof (struct nlm_obj_tdata);
  429. new_tdata = bfd_zalloc (abfd, amt);
  430. if (new_tdata == NULL)
  431. goto got_no_match;
  432. nlm_tdata (abfd) = new_tdata;
  433. i_fxdhdrp = nlm_fixed_header (abfd);
  434. nlm_swap_fixed_header_in (abfd, x_fxdhdr, i_fxdhdrp);
  435. free (x_fxdhdr);
  436. x_fxdhdr = NULL;
  437. /* Check to see if we have an NLM file for this backend by matching
  438. the NLM signature. */
  439. signature = nlm_signature (abfd);
  440. if (signature != NULL
  441. && *signature != '\0'
  442. && strncmp ((char *) i_fxdhdrp->signature, signature,
  443. NLM_SIGNATURE_SIZE) != 0)
  444. goto got_wrong_format_error;
  445. /* There's no supported way to discover the endianness of an NLM, so test for
  446. a sane version number after doing byte swapping appropriate for this
  447. XVEC. (Hack alert!) */
  448. if (i_fxdhdrp->version > 0xFFFF)
  449. goto got_wrong_format_error;
  450. /* There's no supported way to check for 32 bit versus 64 bit addresses,
  451. so ignore this distinction for now. (FIXME) */
  452. /* Swap in the rest of the required header. */
  453. if (!nlm_swap_variable_header_in (abfd))
  454. {
  455. if (bfd_get_error () != bfd_error_system_call)
  456. goto got_wrong_format_error;
  457. else
  458. goto got_no_match;
  459. }
  460. /* Add the sections supplied by all NLM's, and then read in the
  461. auxiliary headers. Reading the auxiliary headers may create
  462. additional sections described in the cygnus_ext header.
  463. From this point on we assume that we have an NLM, and do not
  464. treat errors as indicating the wrong format. */
  465. if (!add_bfd_section (abfd, NLM_CODE_NAME,
  466. i_fxdhdrp->codeImageOffset,
  467. i_fxdhdrp->codeImageSize,
  468. (SEC_CODE | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  469. | SEC_RELOC))
  470. || !add_bfd_section (abfd, NLM_INITIALIZED_DATA_NAME,
  471. i_fxdhdrp->dataImageOffset,
  472. i_fxdhdrp->dataImageSize,
  473. (SEC_DATA | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  474. | SEC_RELOC))
  475. || !add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
  476. (file_ptr) 0,
  477. i_fxdhdrp->uninitializedDataSize,
  478. SEC_ALLOC))
  479. goto got_no_match;
  480. if (!nlm_swap_auxiliary_headers_in (abfd))
  481. goto got_no_match;
  482. if (nlm_fixed_header (abfd)->numberOfRelocationFixups != 0
  483. || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
  484. abfd->flags |= HAS_RELOC;
  485. if (nlm_fixed_header (abfd)->numberOfPublics != 0
  486. || nlm_fixed_header (abfd)->numberOfDebugRecords != 0
  487. || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
  488. abfd->flags |= HAS_SYMS;
  489. arch = nlm_architecture (abfd);
  490. if (arch != bfd_arch_unknown)
  491. bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
  492. abfd->flags |= EXEC_P;
  493. bfd_get_start_address (abfd) = nlm_fixed_header (abfd)->codeStartOffset;
  494. return abfd->xvec;
  495. got_wrong_format_error:
  496. bfd_set_error (bfd_error_wrong_format);
  497. got_no_match:
  498. nlm_tdata (abfd) = preserved_tdata;
  499. if (new_tdata != NULL)
  500. bfd_release (abfd, new_tdata);
  501. if (x_fxdhdr != NULL)
  502. free (x_fxdhdr);
  503. return NULL;
  504. }
  505. /* Swap and write out the variable length header. All the fields must
  506. exist in the NLM, and must exist in this order. */
  507. static bfd_boolean
  508. nlm_swap_variable_header_out (bfd *abfd)
  509. {
  510. bfd_byte temp[NLM_TARGET_LONG_SIZE];
  511. bfd_size_type amt;
  512. /* Write the description length and text members. */
  513. amt = sizeof (nlm_variable_header (abfd)->descriptionLength);
  514. if (bfd_bwrite (& nlm_variable_header (abfd)->descriptionLength, amt,
  515. abfd) != amt)
  516. return FALSE;
  517. amt = nlm_variable_header (abfd)->descriptionLength + 1;
  518. if (bfd_bwrite ((void *) nlm_variable_header (abfd)->descriptionText, amt,
  519. abfd) != amt)
  520. return FALSE;
  521. /* Convert and write the stackSize field. */
  522. put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->stackSize, temp);
  523. amt = sizeof (temp);
  524. if (bfd_bwrite (temp, amt, abfd) != amt)
  525. return FALSE;
  526. /* Convert and write the reserved field. */
  527. put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->reserved, temp);
  528. amt = sizeof (temp);
  529. if (bfd_bwrite (temp, amt, abfd) != amt)
  530. return FALSE;
  531. /* Write the oldThreadName field. This field is a fixed length string. */
  532. amt = sizeof (nlm_variable_header (abfd)->oldThreadName);
  533. if (bfd_bwrite (nlm_variable_header (abfd)->oldThreadName, amt,
  534. abfd) != amt)
  535. return FALSE;
  536. /* Write the screen name length and text members. */
  537. amt = sizeof (nlm_variable_header (abfd)->screenNameLength);
  538. if (bfd_bwrite (& nlm_variable_header (abfd)->screenNameLength, amt,
  539. abfd) != amt)
  540. return FALSE;
  541. amt = nlm_variable_header (abfd)->screenNameLength + 1;
  542. if (bfd_bwrite (nlm_variable_header (abfd)->screenName, amt, abfd) != amt)
  543. return FALSE;
  544. /* Write the thread name length and text members. */
  545. amt = sizeof (nlm_variable_header (abfd)->threadNameLength);
  546. if (bfd_bwrite (& nlm_variable_header (abfd)->threadNameLength, amt,
  547. abfd) != amt)
  548. return FALSE;
  549. amt = nlm_variable_header (abfd)->threadNameLength + 1;
  550. if (bfd_bwrite (nlm_variable_header (abfd)->threadName, amt, abfd) != amt)
  551. return FALSE;
  552. return TRUE;
  553. }
  554. /* Return whether there is a non-zero byte in a memory block. */
  555. static bfd_boolean
  556. find_nonzero (void * buf, size_t size)
  557. {
  558. char *p = (char *) buf;
  559. while (size-- != 0)
  560. if (*p++ != 0)
  561. return TRUE;
  562. return FALSE;
  563. }
  564. /* Swap out the contents of the auxiliary headers. We create those
  565. auxiliary headers which have been set non-zero. We do not require
  566. the caller to set up the stamp fields. */
  567. static bfd_boolean
  568. nlm_swap_auxiliary_headers_out (bfd *abfd)
  569. {
  570. bfd_size_type amt;
  571. /* Write out the version header if there is one. */
  572. if (find_nonzero (nlm_version_header (abfd),
  573. sizeof (Nlm_Internal_Version_Header)))
  574. {
  575. Nlm_External_Version_Header thdr;
  576. LITMEMCPY (thdr.stamp, "VeRsIoN#");
  577. put_word (abfd, (bfd_vma) nlm_version_header (abfd)->majorVersion,
  578. (bfd_byte *) thdr.majorVersion);
  579. put_word (abfd, (bfd_vma) nlm_version_header (abfd)->minorVersion,
  580. (bfd_byte *) thdr.minorVersion);
  581. put_word (abfd, (bfd_vma) nlm_version_header (abfd)->revision,
  582. (bfd_byte *) thdr.revision);
  583. put_word (abfd, (bfd_vma) nlm_version_header (abfd)->year,
  584. (bfd_byte *) thdr.year);
  585. put_word (abfd, (bfd_vma) nlm_version_header (abfd)->month,
  586. (bfd_byte *) thdr.month);
  587. put_word (abfd, (bfd_vma) nlm_version_header (abfd)->day,
  588. (bfd_byte *) thdr.day);
  589. if (bfd_bwrite ((void *) &thdr, (bfd_size_type) sizeof (thdr), abfd)
  590. != sizeof (thdr))
  591. return FALSE;
  592. }
  593. /* Note - the CoPyRiGhT tag is emitted before the MeSsAgEs
  594. tag in order to make the NW4.x and NW5.x loaders happy. */
  595. /* Write out the copyright header if there is one. */
  596. if (find_nonzero (nlm_copyright_header (abfd),
  597. sizeof (Nlm_Internal_Copyright_Header)))
  598. {
  599. Nlm_External_Copyright_Header thdr;
  600. LITMEMCPY (thdr.stamp, "CoPyRiGhT=");
  601. amt = sizeof (thdr.stamp);
  602. if (bfd_bwrite ((void *) thdr.stamp, amt, abfd) != amt)
  603. return FALSE;
  604. thdr.copyrightMessageLength[0] =
  605. nlm_copyright_header (abfd)->copyrightMessageLength;
  606. amt = 1;
  607. if (bfd_bwrite ((void *) thdr.copyrightMessageLength, amt, abfd) != amt)
  608. return FALSE;
  609. /* The copyright message is a variable length string. */
  610. amt = nlm_copyright_header (abfd)->copyrightMessageLength + 1;
  611. if (bfd_bwrite ((void *) nlm_copyright_header (abfd)->copyrightMessage,
  612. amt, abfd) != amt)
  613. return FALSE;
  614. }
  615. /* Write out the extended header if there is one. */
  616. if (find_nonzero (nlm_extended_header (abfd),
  617. sizeof (Nlm_Internal_Extended_Header)))
  618. {
  619. Nlm_External_Extended_Header thdr;
  620. LITMEMCPY (thdr.stamp, "MeSsAgEs");
  621. put_word (abfd,
  622. (bfd_vma) nlm_extended_header (abfd)->languageID,
  623. (bfd_byte *) thdr.languageID);
  624. put_word (abfd,
  625. (bfd_vma) nlm_extended_header (abfd)->messageFileOffset,
  626. (bfd_byte *) thdr.messageFileOffset);
  627. put_word (abfd,
  628. (bfd_vma) nlm_extended_header (abfd)->messageFileLength,
  629. (bfd_byte *) thdr.messageFileLength);
  630. put_word (abfd,
  631. (bfd_vma) nlm_extended_header (abfd)->messageCount,
  632. (bfd_byte *) thdr.messageCount);
  633. put_word (abfd,
  634. (bfd_vma) nlm_extended_header (abfd)->helpFileOffset,
  635. (bfd_byte *) thdr.helpFileOffset);
  636. put_word (abfd,
  637. (bfd_vma) nlm_extended_header (abfd)->helpFileLength,
  638. (bfd_byte *) thdr.helpFileLength);
  639. put_word (abfd,
  640. (bfd_vma) nlm_extended_header (abfd)->RPCDataOffset,
  641. (bfd_byte *) thdr.RPCDataOffset);
  642. put_word (abfd,
  643. (bfd_vma) nlm_extended_header (abfd)->RPCDataLength,
  644. (bfd_byte *) thdr.RPCDataLength);
  645. put_word (abfd,
  646. (bfd_vma) nlm_extended_header (abfd)->sharedCodeOffset,
  647. (bfd_byte *) thdr.sharedCodeOffset);
  648. put_word (abfd,
  649. (bfd_vma) nlm_extended_header (abfd)->sharedCodeLength,
  650. (bfd_byte *) thdr.sharedCodeLength);
  651. put_word (abfd,
  652. (bfd_vma) nlm_extended_header (abfd)->sharedDataOffset,
  653. (bfd_byte *) thdr.sharedDataOffset);
  654. put_word (abfd,
  655. (bfd_vma) nlm_extended_header (abfd)->sharedDataLength,
  656. (bfd_byte *) thdr.sharedDataLength);
  657. put_word (abfd,
  658. (bfd_vma) nlm_extended_header (abfd)->sharedRelocationFixupOffset,
  659. (bfd_byte *) thdr.sharedRelocationFixupOffset);
  660. put_word (abfd,
  661. (bfd_vma) nlm_extended_header (abfd)->sharedRelocationFixupCount,
  662. (bfd_byte *) thdr.sharedRelocationFixupCount);
  663. put_word (abfd,
  664. (bfd_vma) nlm_extended_header (abfd)->sharedExternalReferenceOffset,
  665. (bfd_byte *) thdr.sharedExternalReferenceOffset);
  666. put_word (abfd,
  667. (bfd_vma) nlm_extended_header (abfd)->sharedExternalReferenceCount,
  668. (bfd_byte *) thdr.sharedExternalReferenceCount);
  669. put_word (abfd,
  670. (bfd_vma) nlm_extended_header (abfd)->sharedPublicsOffset,
  671. (bfd_byte *) thdr.sharedPublicsOffset);
  672. put_word (abfd,
  673. (bfd_vma) nlm_extended_header (abfd)->sharedPublicsCount,
  674. (bfd_byte *) thdr.sharedPublicsCount);
  675. put_word (abfd,
  676. (bfd_vma) nlm_extended_header (abfd)->sharedDebugRecordOffset,
  677. (bfd_byte *) thdr.sharedDebugRecordOffset);
  678. put_word (abfd,
  679. (bfd_vma) nlm_extended_header (abfd)->sharedDebugRecordCount,
  680. (bfd_byte *) thdr.sharedDebugRecordCount);
  681. put_word (abfd,
  682. (bfd_vma) nlm_extended_header (abfd)->SharedInitializationOffset,
  683. (bfd_byte *) thdr.sharedInitializationOffset);
  684. put_word (abfd,
  685. (bfd_vma) nlm_extended_header (abfd)->SharedExitProcedureOffset,
  686. (bfd_byte *) thdr.SharedExitProcedureOffset);
  687. put_word (abfd,
  688. (bfd_vma) nlm_extended_header (abfd)->productID,
  689. (bfd_byte *) thdr.productID);
  690. put_word (abfd,
  691. (bfd_vma) nlm_extended_header (abfd)->reserved0,
  692. (bfd_byte *) thdr.reserved0);
  693. put_word (abfd,
  694. (bfd_vma) nlm_extended_header (abfd)->reserved1,
  695. (bfd_byte *) thdr.reserved1);
  696. put_word (abfd,
  697. (bfd_vma) nlm_extended_header (abfd)->reserved2,
  698. (bfd_byte *) thdr.reserved2);
  699. put_word (abfd,
  700. (bfd_vma) nlm_extended_header (abfd)->reserved3,
  701. (bfd_byte *) thdr.reserved3);
  702. put_word (abfd,
  703. (bfd_vma) nlm_extended_header (abfd)->reserved4,
  704. (bfd_byte *) thdr.reserved4);
  705. put_word (abfd,
  706. (bfd_vma) nlm_extended_header (abfd)->reserved5,
  707. (bfd_byte *) thdr.reserved5);
  708. if (bfd_bwrite ((void *) &thdr, (bfd_size_type) sizeof (thdr), abfd)
  709. != sizeof (thdr))
  710. return FALSE;
  711. }
  712. /* Write out the custom header if there is one. */
  713. if (find_nonzero (nlm_custom_header (abfd),
  714. sizeof (Nlm_Internal_Custom_Header)))
  715. {
  716. Nlm_External_Custom_Header thdr;
  717. bfd_boolean ds;
  718. bfd_size_type hdrLength;
  719. ds = find_nonzero (nlm_custom_header (abfd)->dataStamp,
  720. sizeof (nlm_custom_header (abfd)->dataStamp));
  721. LITMEMCPY (thdr.stamp, "CuStHeAd");
  722. hdrLength = (2 * NLM_TARGET_LONG_SIZE + (ds ? 8 : 0)
  723. + nlm_custom_header (abfd)->hdrLength);
  724. put_word (abfd, hdrLength, thdr.length);
  725. put_word (abfd, (bfd_vma) nlm_custom_header (abfd)->dataOffset,
  726. thdr.dataOffset);
  727. put_word (abfd, (bfd_vma) nlm_custom_header (abfd)->dataLength,
  728. thdr.dataLength);
  729. if (! ds)
  730. {
  731. BFD_ASSERT (nlm_custom_header (abfd)->hdrLength == 0);
  732. amt = sizeof (thdr) - sizeof (thdr.dataStamp);
  733. if (bfd_bwrite ((void *) &thdr, amt, abfd) != amt)
  734. return FALSE;
  735. }
  736. else
  737. {
  738. memcpy (thdr.dataStamp, nlm_custom_header (abfd)->dataStamp,
  739. sizeof (thdr.dataStamp));
  740. amt = sizeof (thdr);
  741. if (bfd_bwrite ((void *) &thdr, amt, abfd) != amt)
  742. return FALSE;
  743. amt = nlm_custom_header (abfd)->hdrLength;
  744. if (bfd_bwrite (nlm_custom_header (abfd)->hdr, amt, abfd) != amt)
  745. return FALSE;
  746. }
  747. }
  748. /* Write out the Cygnus debugging header if there is one. */
  749. if (find_nonzero (nlm_cygnus_ext_header (abfd),
  750. sizeof (Nlm_Internal_Cygnus_Ext_Header)))
  751. {
  752. Nlm_External_Custom_Header thdr;
  753. LITMEMCPY (thdr.stamp, "CuStHeAd");
  754. put_word (abfd, (bfd_vma) 2 * NLM_TARGET_LONG_SIZE + 8,
  755. (bfd_byte *) thdr.length);
  756. put_word (abfd, (bfd_vma) nlm_cygnus_ext_header (abfd)->offset,
  757. (bfd_byte *) thdr.dataOffset);
  758. put_word (abfd, (bfd_vma) nlm_cygnus_ext_header (abfd)->length,
  759. (bfd_byte *) thdr.dataLength);
  760. LITMEMCPY (thdr.dataStamp, "CyGnUsEx");
  761. amt = sizeof (thdr);
  762. if (bfd_bwrite ((void *) &thdr, amt, abfd) != amt)
  763. return FALSE;
  764. }
  765. return TRUE;
  766. }
  767. /* We read the NLM's public symbols and use it to generate a bfd symbol
  768. table (hey, it's better than nothing) on a one-for-one basis. Thus
  769. use the number of public symbols as the number of bfd symbols we will
  770. have once we actually get around to reading them in.
  771. Return the number of bytes required to hold the symtab vector, based on
  772. the count plus 1, since we will NULL terminate the vector allocated based
  773. on this size. */
  774. long
  775. nlm_get_symtab_upper_bound (bfd *abfd)
  776. {
  777. Nlm_Internal_Fixed_Header *i_fxdhdrp; /* Nlm file header, internal form. */
  778. long symcount;
  779. long symtab_size = 0;
  780. i_fxdhdrp = nlm_fixed_header (abfd);
  781. symcount = (i_fxdhdrp->numberOfPublics
  782. + i_fxdhdrp->numberOfDebugRecords
  783. + i_fxdhdrp->numberOfExternalReferences);
  784. symtab_size = (symcount + 1) * (sizeof (asymbol));
  785. return symtab_size;
  786. }
  787. /* Slurp in nlm symbol table.
  788. In the external (in-file) form, NLM export records are variable length,
  789. with the following form:
  790. 1 byte length of the symbol name (N)
  791. N bytes the symbol name
  792. 4 bytes the symbol offset from start of it's section
  793. We also read in the debugging symbols and import records. Import
  794. records are treated as undefined symbols. As we read the import
  795. records we also read in the associated reloc information, which is
  796. attached to the symbol.
  797. The bfd symbols are copied to SYMvoid *S.
  798. When we return, the bfd symcount is either zero or contains the correct
  799. number of symbols. */
  800. static bfd_boolean
  801. nlm_slurp_symbol_table (bfd *abfd)
  802. {
  803. Nlm_Internal_Fixed_Header *i_fxdhdrp; /* Nlm file header, internal form. */
  804. bfd_size_type totsymcount; /* Number of NLM symbols. */
  805. bfd_size_type symcount; /* Counter of NLM symbols. */
  806. nlm_symbol_type *sym; /* Pointer to current bfd symbol. */
  807. unsigned char symlength; /* Symbol length read into here. */
  808. unsigned char symtype; /* Type of debugging symbol. */
  809. bfd_byte temp[NLM_TARGET_LONG_SIZE]; /* Symbol offsets read into here. */
  810. bfd_boolean (*read_import_func) (bfd *, nlm_symbol_type *);
  811. bfd_boolean (*set_public_section_func) (bfd *, nlm_symbol_type *);
  812. bfd_size_type amt;
  813. if (nlm_get_symbols (abfd) != NULL)
  814. return TRUE;
  815. /* Read each raw NLM symbol, using the information to create a canonical bfd
  816. symbol table entry.
  817. Note that we allocate the initial bfd canonical symbol buffer based on a
  818. one-to-one mapping of the NLM symbols to canonical symbols. We actually
  819. use all the NLM symbols, so there will be no space left over at the end.
  820. When we have all the symbols, we build the caller's pointer vector. */
  821. abfd->symcount = 0;
  822. i_fxdhdrp = nlm_fixed_header (abfd);
  823. totsymcount = (i_fxdhdrp->numberOfPublics
  824. + i_fxdhdrp->numberOfDebugRecords
  825. + i_fxdhdrp->numberOfExternalReferences);
  826. if (totsymcount == 0)
  827. return TRUE;
  828. if (bfd_seek (abfd, i_fxdhdrp->publicsOffset, SEEK_SET) != 0)
  829. return FALSE;
  830. amt = totsymcount * sizeof (nlm_symbol_type);
  831. sym = bfd_zalloc (abfd, amt);
  832. if (!sym)
  833. return FALSE;
  834. nlm_set_symbols (abfd, sym);
  835. /* We use the bfd's symcount directly as the control count, so that early
  836. termination of the loop leaves the symcount correct for the symbols that
  837. were read. */
  838. set_public_section_func = nlm_set_public_section_func (abfd);
  839. symcount = i_fxdhdrp->numberOfPublics;
  840. while (abfd->symcount < symcount)
  841. {
  842. amt = sizeof (symlength);
  843. if (bfd_bread ((void *) &symlength, amt, abfd) != amt)
  844. return FALSE;
  845. amt = symlength;
  846. sym->symbol.the_bfd = abfd;
  847. sym->symbol.name = bfd_alloc (abfd, amt + 1);
  848. if (!sym->symbol.name)
  849. return FALSE;
  850. if (bfd_bread ((void *) sym->symbol.name, amt, abfd) != amt)
  851. return FALSE;
  852. /* Cast away const. */
  853. ((char *) (sym->symbol.name))[symlength] = '\0';
  854. amt = sizeof (temp);
  855. if (bfd_bread ((void *) temp, amt, abfd) != amt)
  856. return FALSE;
  857. sym->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
  858. sym->symbol.value = get_word (abfd, temp);
  859. if (set_public_section_func)
  860. {
  861. /* Most backends can use the code below, but unfortunately
  862. some use a different scheme. */
  863. if (! (*set_public_section_func) (abfd, sym))
  864. return FALSE;
  865. }
  866. else
  867. {
  868. if (sym->symbol.value & NLM_HIBIT)
  869. {
  870. sym->symbol.value &= ~NLM_HIBIT;
  871. sym->symbol.flags |= BSF_FUNCTION;
  872. sym->symbol.section =
  873. bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  874. }
  875. else
  876. sym->symbol.section =
  877. bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  878. }
  879. sym->rcnt = 0;
  880. abfd->symcount++;
  881. sym++;
  882. }
  883. /* Read the debugging records. */
  884. if (i_fxdhdrp->numberOfDebugRecords > 0)
  885. {
  886. if (bfd_seek (abfd, i_fxdhdrp->debugInfoOffset, SEEK_SET) != 0)
  887. return FALSE;
  888. symcount += i_fxdhdrp->numberOfDebugRecords;
  889. while (abfd->symcount < symcount)
  890. {
  891. amt = sizeof (symtype);
  892. if (bfd_bread ((void *) &symtype, amt, abfd) != amt)
  893. return FALSE;
  894. amt = sizeof (temp);
  895. if (bfd_bread ((void *) temp, amt, abfd) != amt)
  896. return FALSE;
  897. amt = sizeof (symlength);
  898. if (bfd_bread ((void *) &symlength, amt, abfd) != amt)
  899. return FALSE;
  900. amt = symlength;
  901. sym->symbol.the_bfd = abfd;
  902. sym->symbol.name = bfd_alloc (abfd, amt + 1);
  903. if (!sym->symbol.name)
  904. return FALSE;
  905. if (bfd_bread ((void *) sym->symbol.name, amt, abfd) != amt)
  906. return FALSE;
  907. /* Cast away const. */
  908. ((char *) (sym->symbol.name))[symlength] = '\0';
  909. sym->symbol.flags = BSF_LOCAL;
  910. sym->symbol.value = get_word (abfd, temp);
  911. if (symtype == 0)
  912. sym->symbol.section =
  913. bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  914. else if (symtype == 1)
  915. {
  916. sym->symbol.flags |= BSF_FUNCTION;
  917. sym->symbol.section =
  918. bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  919. }
  920. else
  921. sym->symbol.section = bfd_abs_section_ptr;
  922. sym->rcnt = 0;
  923. abfd->symcount++;
  924. sym++;
  925. }
  926. }
  927. /* Read in the import records. We can only do this if we know how
  928. to read relocs for this target. */
  929. read_import_func = nlm_read_import_func (abfd);
  930. if (read_import_func != NULL)
  931. {
  932. if (bfd_seek (abfd, i_fxdhdrp->externalReferencesOffset, SEEK_SET) != 0)
  933. return FALSE;
  934. symcount += i_fxdhdrp->numberOfExternalReferences;
  935. while (abfd->symcount < symcount)
  936. {
  937. if (! (*read_import_func) (abfd, sym))
  938. return FALSE;
  939. sym++;
  940. abfd->symcount++;
  941. }
  942. }
  943. return TRUE;
  944. }
  945. /* Note that bfd_get_symcount is guaranteed to be zero if slurping the
  946. symbol table fails. */
  947. long
  948. nlm_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  949. {
  950. nlm_symbol_type *symbase;
  951. bfd_size_type counter = 0;
  952. if (! nlm_slurp_symbol_table (abfd))
  953. return -1;
  954. symbase = nlm_get_symbols (abfd);
  955. while (counter < bfd_get_symcount (abfd))
  956. {
  957. *alocation++ = &symbase->symbol;
  958. symbase++;
  959. counter++;
  960. }
  961. *alocation = NULL;
  962. return bfd_get_symcount (abfd);
  963. }
  964. /* Make an NLM symbol. There is nothing special to do here. */
  965. asymbol *
  966. nlm_make_empty_symbol (bfd *abfd)
  967. {
  968. bfd_size_type amt = sizeof (nlm_symbol_type);
  969. nlm_symbol_type *new = bfd_zalloc (abfd, amt);
  970. if (new == NULL)
  971. return NULL;
  972. new->symbol.the_bfd = abfd;
  973. return & new->symbol;
  974. }
  975. /* Get symbol information. */
  976. void
  977. nlm_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  978. asymbol *symbol,
  979. symbol_info *ret)
  980. {
  981. bfd_symbol_info (symbol, ret);
  982. }
  983. /* Print symbol information. */
  984. void
  985. nlm_print_symbol (bfd *abfd,
  986. void * afile,
  987. asymbol *symbol,
  988. bfd_print_symbol_type how)
  989. {
  990. FILE *file = (FILE *) afile;
  991. switch (how)
  992. {
  993. case bfd_print_symbol_name:
  994. case bfd_print_symbol_more:
  995. if (symbol->name)
  996. fprintf (file, "%s", symbol->name);
  997. break;
  998. case bfd_print_symbol_all:
  999. bfd_print_symbol_vandf (abfd, (void *) file, symbol);
  1000. fprintf (file, " %-5s", symbol->section->name);
  1001. if (symbol->name)
  1002. fprintf (file, " %s", symbol->name);
  1003. break;
  1004. }
  1005. }
  1006. /* Get the relocs for an NLM file. There are two types of relocs.
  1007. Imports are relocs against symbols defined in other NLM files. We
  1008. treat these as relocs against global symbols. Relocation fixups
  1009. are internal relocs.
  1010. The actual format used to store the relocs is machine specific. */
  1011. /* Read in the relocation fixup information. This is stored in
  1012. nlm_relocation_fixups, an array of arelent structures, and
  1013. nlm_relocation_fixup_secs, an array of section pointers. The
  1014. section pointers are needed because the relocs are not sorted by
  1015. section. */
  1016. static bfd_boolean
  1017. nlm_slurp_reloc_fixups (bfd *abfd)
  1018. {
  1019. bfd_boolean (*read_func) (bfd *, nlm_symbol_type *, asection **, arelent *);
  1020. bfd_size_type count, amt;
  1021. arelent *rels;
  1022. asection **secs;
  1023. if (nlm_relocation_fixups (abfd) != NULL)
  1024. return TRUE;
  1025. read_func = nlm_read_reloc_func (abfd);
  1026. if (read_func == NULL)
  1027. return TRUE;
  1028. if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
  1029. SEEK_SET) != 0)
  1030. return FALSE;
  1031. count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
  1032. amt = count * sizeof (arelent);
  1033. rels = bfd_alloc (abfd, amt);
  1034. amt = count * sizeof (asection *);
  1035. secs = bfd_alloc (abfd, amt);
  1036. if ((rels == NULL || secs == NULL) && count != 0)
  1037. return FALSE;
  1038. nlm_relocation_fixups (abfd) = rels;
  1039. nlm_relocation_fixup_secs (abfd) = secs;
  1040. /* We have to read piece by piece, because we don't know how large
  1041. the machine specific reloc information is. */
  1042. while (count-- != 0)
  1043. {
  1044. if (! (*read_func) (abfd, NULL, secs, rels))
  1045. {
  1046. nlm_relocation_fixups (abfd) = NULL;
  1047. nlm_relocation_fixup_secs (abfd) = NULL;
  1048. return FALSE;
  1049. }
  1050. ++secs;
  1051. ++rels;
  1052. }
  1053. return TRUE;
  1054. }
  1055. /* Get the number of relocs. This really just returns an upper bound,
  1056. since it does not attempt to distinguish them based on the section.
  1057. That will be handled when they are actually read. */
  1058. long
  1059. nlm_get_reloc_upper_bound (bfd *abfd, asection *sec)
  1060. {
  1061. nlm_symbol_type *syms;
  1062. bfd_size_type count;
  1063. unsigned int ret;
  1064. /* If we don't know how to read relocs, just return 0. */
  1065. if (nlm_read_reloc_func (abfd) == NULL)
  1066. return -1;
  1067. /* Make sure we have either the code or the data section. */
  1068. if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
  1069. return 0;
  1070. syms = nlm_get_symbols (abfd);
  1071. if (syms == NULL)
  1072. {
  1073. if (! nlm_slurp_symbol_table (abfd))
  1074. return -1;
  1075. syms = nlm_get_symbols (abfd);
  1076. }
  1077. ret = nlm_fixed_header (abfd)->numberOfRelocationFixups;
  1078. count = bfd_get_symcount (abfd);
  1079. while (count-- != 0)
  1080. {
  1081. ret += syms->rcnt;
  1082. ++syms;
  1083. }
  1084. return (ret + 1) * sizeof (arelent *);
  1085. }
  1086. /* Get the relocs themselves. */
  1087. long
  1088. nlm_canonicalize_reloc (bfd *abfd,
  1089. asection *sec,
  1090. arelent **relptr,
  1091. asymbol **symbols)
  1092. {
  1093. arelent *rels;
  1094. asection **secs;
  1095. bfd_size_type count, i;
  1096. long ret;
  1097. /* Get the relocation fixups. */
  1098. rels = nlm_relocation_fixups (abfd);
  1099. if (rels == NULL)
  1100. {
  1101. if (! nlm_slurp_reloc_fixups (abfd))
  1102. return -1;
  1103. rels = nlm_relocation_fixups (abfd);
  1104. }
  1105. secs = nlm_relocation_fixup_secs (abfd);
  1106. ret = 0;
  1107. count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
  1108. for (i = 0; i < count; i++, rels++, secs++)
  1109. {
  1110. if (*secs == sec)
  1111. {
  1112. *relptr++ = rels;
  1113. ++ret;
  1114. }
  1115. }
  1116. /* Get the import symbols. */
  1117. count = bfd_get_symcount (abfd);
  1118. for (i = 0; i < count; i++, symbols++)
  1119. {
  1120. asymbol *sym;
  1121. sym = *symbols;
  1122. if (bfd_asymbol_flavour (sym) == bfd_target_nlm_flavour)
  1123. {
  1124. nlm_symbol_type *nlm_sym;
  1125. bfd_size_type j;
  1126. nlm_sym = (nlm_symbol_type *) sym;
  1127. for (j = 0; j < nlm_sym->rcnt; j++)
  1128. {
  1129. if (nlm_sym->relocs[j].section == sec)
  1130. {
  1131. *relptr = &nlm_sym->relocs[j].reloc;
  1132. (*relptr)->sym_ptr_ptr = symbols;
  1133. ++relptr;
  1134. ++ret;
  1135. }
  1136. }
  1137. }
  1138. }
  1139. *relptr = NULL;
  1140. return ret;
  1141. }
  1142. /* Compute the section file positions for an NLM file. All variable
  1143. length data in the file headers must be set before this function is
  1144. called. If the variable length data is changed later, the
  1145. resulting object file will be incorrect. Unfortunately, there is
  1146. no way to check this.
  1147. This routine also sets the Size and Offset fields in the fixed
  1148. header.
  1149. It also looks over the symbols and moves any common symbols into
  1150. the .bss section; NLM has no way to represent a common symbol.
  1151. This approach means that either the symbols must already have been
  1152. set at this point, or there must be no common symbols. We need to
  1153. move the symbols at this point so that mangle_relocs can see the
  1154. final values. */
  1155. static bfd_boolean
  1156. nlm_compute_section_file_positions (bfd *abfd)
  1157. {
  1158. file_ptr sofar;
  1159. asection *sec;
  1160. bfd_vma text, data, bss;
  1161. bfd_vma text_low, data_low;
  1162. unsigned int text_align, data_align, other_align;
  1163. file_ptr text_ptr, data_ptr, other_ptr;
  1164. asection *bss_sec;
  1165. asymbol **sym_ptr_ptr;
  1166. if (abfd->output_has_begun)
  1167. return TRUE;
  1168. /* Make sure we have a section to hold uninitialized data. */
  1169. bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
  1170. if (bss_sec == NULL)
  1171. {
  1172. if (!add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
  1173. (file_ptr) 0, (bfd_size_type) 0,
  1174. SEC_ALLOC))
  1175. return FALSE;
  1176. bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
  1177. }
  1178. abfd->output_has_begun = TRUE;
  1179. /* The fixed header. */
  1180. sofar = nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd);
  1181. /* The variable header. */
  1182. sofar += (sizeof (nlm_variable_header (abfd)->descriptionLength)
  1183. + nlm_variable_header (abfd)->descriptionLength + 1
  1184. + NLM_TARGET_LONG_SIZE /* stackSize */
  1185. + NLM_TARGET_LONG_SIZE /* reserved */
  1186. + sizeof (nlm_variable_header (abfd)->oldThreadName)
  1187. + sizeof (nlm_variable_header (abfd)->screenNameLength)
  1188. + nlm_variable_header (abfd)->screenNameLength + 1
  1189. + sizeof (nlm_variable_header (abfd)->threadNameLength)
  1190. + nlm_variable_header (abfd)->threadNameLength + 1);
  1191. /* The auxiliary headers. */
  1192. if (find_nonzero (nlm_version_header (abfd),
  1193. sizeof (Nlm_Internal_Version_Header)))
  1194. sofar += sizeof (Nlm_External_Version_Header);
  1195. if (find_nonzero (nlm_extended_header (abfd),
  1196. sizeof (Nlm_Internal_Extended_Header)))
  1197. sofar += sizeof (Nlm_External_Extended_Header);
  1198. if (find_nonzero (nlm_copyright_header (abfd),
  1199. sizeof (Nlm_Internal_Copyright_Header)))
  1200. sofar += (sizeof (Nlm_External_Copyright_Header)
  1201. + nlm_copyright_header (abfd)->copyrightMessageLength + 1);
  1202. if (find_nonzero (nlm_custom_header (abfd),
  1203. sizeof (Nlm_Internal_Custom_Header)))
  1204. sofar += (sizeof (Nlm_External_Custom_Header)
  1205. + nlm_custom_header (abfd)->hdrLength);
  1206. if (find_nonzero (nlm_cygnus_ext_header (abfd),
  1207. sizeof (Nlm_Internal_Cygnus_Ext_Header)))
  1208. sofar += sizeof (Nlm_External_Custom_Header);
  1209. /* Compute the section file positions in two passes. First get the
  1210. sizes of the text and data sections, and then set the file
  1211. positions. This code aligns the sections in the file using the
  1212. same alignment restrictions that apply to the sections in memory;
  1213. this may not be necessary. */
  1214. text = 0;
  1215. text_low = (bfd_vma) - 1;
  1216. text_align = 0;
  1217. data = 0;
  1218. data_low = (bfd_vma) - 1;
  1219. data_align = 0;
  1220. bss = 0;
  1221. other_align = 0;
  1222. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  1223. {
  1224. flagword f;
  1225. sec->size = BFD_ALIGN (sec->size, 1 << sec->alignment_power);
  1226. f = bfd_get_section_flags (abfd, sec);
  1227. if (f & SEC_CODE)
  1228. {
  1229. text += sec->size;
  1230. if (bfd_get_section_vma (abfd, sec) < text_low)
  1231. text_low = bfd_get_section_vma (abfd, sec);
  1232. if (sec->alignment_power > text_align)
  1233. text_align = sec->alignment_power;
  1234. }
  1235. else if (f & SEC_DATA)
  1236. {
  1237. data += sec->size;
  1238. if (bfd_get_section_vma (abfd, sec) < data_low)
  1239. data_low = bfd_get_section_vma (abfd, sec);
  1240. if (sec->alignment_power > data_align)
  1241. data_align = sec->alignment_power;
  1242. }
  1243. else if (f & SEC_HAS_CONTENTS)
  1244. {
  1245. if (sec->alignment_power > other_align)
  1246. other_align = sec->alignment_power;
  1247. }
  1248. else if (f & SEC_ALLOC)
  1249. bss += sec->size;
  1250. }
  1251. nlm_set_text_low (abfd, text_low);
  1252. nlm_set_data_low (abfd, data_low);
  1253. if (nlm_no_uninitialized_data (abfd))
  1254. {
  1255. /* This NetWare format does not use uninitialized data. We must
  1256. increase the size of the data section. We will never wind up
  1257. writing those file locations, so they will remain zero. */
  1258. data += bss;
  1259. bss = 0;
  1260. }
  1261. text_ptr = BFD_ALIGN (sofar, 1 << text_align);
  1262. data_ptr = BFD_ALIGN (text_ptr + text, 1 << data_align);
  1263. other_ptr = BFD_ALIGN (data_ptr + data, 1 << other_align);
  1264. /* Fill in some fields in the header for which we now have the
  1265. information. */
  1266. nlm_fixed_header (abfd)->codeImageOffset = text_ptr;
  1267. nlm_fixed_header (abfd)->codeImageSize = text;
  1268. nlm_fixed_header (abfd)->dataImageOffset = data_ptr;
  1269. nlm_fixed_header (abfd)->dataImageSize = data;
  1270. nlm_fixed_header (abfd)->uninitializedDataSize = bss;
  1271. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  1272. {
  1273. flagword f;
  1274. f = bfd_get_section_flags (abfd, sec);
  1275. if (f & SEC_CODE)
  1276. {
  1277. sec->filepos = text_ptr;
  1278. text_ptr += sec->size;
  1279. }
  1280. else if (f & SEC_DATA)
  1281. {
  1282. sec->filepos = data_ptr;
  1283. data_ptr += sec->size;
  1284. }
  1285. else if (f & SEC_HAS_CONTENTS)
  1286. {
  1287. sec->filepos = other_ptr;
  1288. other_ptr += sec->size;
  1289. }
  1290. }
  1291. nlm_fixed_header (abfd)->relocationFixupOffset = other_ptr;
  1292. /* Move all common symbols into the .bss section. */
  1293. sym_ptr_ptr = bfd_get_outsymbols (abfd);
  1294. if (sym_ptr_ptr != NULL)
  1295. {
  1296. asymbol **sym_end;
  1297. bfd_vma add;
  1298. sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
  1299. add = 0;
  1300. for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
  1301. {
  1302. asymbol *sym;
  1303. bfd_vma size;
  1304. sym = *sym_ptr_ptr;
  1305. if (!bfd_is_com_section (bfd_get_section (sym)))
  1306. continue;
  1307. /* Put the common symbol in the .bss section, and increase
  1308. the size of the .bss section by the size of the common
  1309. symbol (which is the old value of the symbol). */
  1310. sym->section = bss_sec;
  1311. size = sym->value;
  1312. sym->value = bss_sec->size + add;
  1313. add += size;
  1314. add = BFD_ALIGN (add, 1 << bss_sec->alignment_power);
  1315. }
  1316. if (add != 0)
  1317. {
  1318. if (nlm_no_uninitialized_data (abfd))
  1319. {
  1320. /* We could handle this case, but so far it hasn't been
  1321. necessary. */
  1322. abort ();
  1323. }
  1324. nlm_fixed_header (abfd)->uninitializedDataSize += add;
  1325. bss_sec->size += add;
  1326. }
  1327. }
  1328. return TRUE;
  1329. }
  1330. /* Set the contents of a section. To do this we need to know where
  1331. the section is going to be located in the output file. That means
  1332. that the sizes of all the sections must be set, and all the
  1333. variable size header information must be known. */
  1334. bfd_boolean
  1335. nlm_set_section_contents (bfd *abfd,
  1336. asection *section,
  1337. const void * location,
  1338. file_ptr offset,
  1339. bfd_size_type count)
  1340. {
  1341. if (! abfd->output_has_begun
  1342. && ! nlm_compute_section_file_positions (abfd))
  1343. return FALSE;
  1344. if (count == 0)
  1345. return TRUE;
  1346. /* i386 NetWare has a very restricted set of relocs. In order for
  1347. objcopy to work, the NLM i386 backend needs a chance to rework
  1348. the section contents so that its set of relocs will work. If all
  1349. the relocs are already acceptable, this will not do anything. */
  1350. if (section->reloc_count != 0)
  1351. {
  1352. bfd_boolean (*mangle_relocs_func)
  1353. (bfd *, asection *, const void *, bfd_vma, bfd_size_type);
  1354. mangle_relocs_func = nlm_mangle_relocs_func (abfd);
  1355. if (mangle_relocs_func != NULL)
  1356. {
  1357. if (!(*mangle_relocs_func) (abfd, section, location,
  1358. (bfd_vma) offset, count))
  1359. return FALSE;
  1360. }
  1361. }
  1362. if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
  1363. || bfd_bwrite (location, count, abfd) != count)
  1364. return FALSE;
  1365. return TRUE;
  1366. }
  1367. /* We need to sort a list of relocs associated with sections when we
  1368. write out the external relocs. */
  1369. static int
  1370. nlm_external_reloc_compare (const void *p1, const void *p2)
  1371. {
  1372. const struct reloc_and_sec *r1 = (const struct reloc_and_sec *) p1;
  1373. const struct reloc_and_sec *r2 = (const struct reloc_and_sec *) p2;
  1374. int cmp;
  1375. cmp = strcmp ((*r1->rel->sym_ptr_ptr)->name,
  1376. (*r2->rel->sym_ptr_ptr)->name);
  1377. if (cmp != 0)
  1378. return cmp;
  1379. /* We sort by address within symbol to make the sort more stable and
  1380. increase the chances that different hosts will generate bit for
  1381. bit equivalent results. */
  1382. return (int) (r1->rel->address - r2->rel->address);
  1383. }
  1384. /* Write out an NLM file. We write out the information in this order:
  1385. fixed header
  1386. variable header
  1387. auxiliary headers
  1388. code sections
  1389. data sections
  1390. other sections (custom data, messages, help, shared NLM, RPC,
  1391. module dependencies)
  1392. relocation fixups
  1393. external references (imports)
  1394. public symbols (exports)
  1395. debugging records
  1396. This is similar to the order used by the NetWare tools; the
  1397. difference is that NetWare puts the sections other than code, data
  1398. and custom data at the end of the NLM. It is convenient for us to
  1399. know where the sections are going to be before worrying about the
  1400. size of the other information.
  1401. By the time this function is called, all the section data should
  1402. have been output using set_section_contents. Note that custom
  1403. data, the message file, the help file, the shared NLM file, the RPC
  1404. data, and the module dependencies are all considered to be
  1405. sections; the caller is responsible for filling in the offset and
  1406. length fields in the NLM headers. The relocation fixups and
  1407. imports are both obtained from the list of relocs attached to each
  1408. section. The exports and debugging records are obtained from the
  1409. list of outsymbols. */
  1410. bfd_boolean
  1411. nlm_write_object_contents (bfd *abfd)
  1412. {
  1413. asection *sec;
  1414. bfd_boolean (*write_import_func) (bfd *, asection *, arelent *);
  1415. bfd_size_type external_reloc_count, internal_reloc_count, i, c;
  1416. struct reloc_and_sec *external_relocs;
  1417. asymbol **sym_ptr_ptr;
  1418. file_ptr last;
  1419. bfd_boolean (*write_prefix_func) (bfd *);
  1420. unsigned char *fixed_header = NULL;
  1421. file_ptr pos;
  1422. bfd_size_type amt;
  1423. fixed_header = bfd_malloc (nlm_fixed_header_size (abfd));
  1424. if (fixed_header == NULL)
  1425. goto error_return;
  1426. if (! abfd->output_has_begun
  1427. && ! nlm_compute_section_file_positions (abfd))
  1428. goto error_return;
  1429. /* Write out the variable length headers. */
  1430. pos = nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd);
  1431. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  1432. goto error_return;
  1433. if (! nlm_swap_variable_header_out (abfd)
  1434. || ! nlm_swap_auxiliary_headers_out (abfd))
  1435. {
  1436. bfd_set_error (bfd_error_system_call);
  1437. goto error_return;
  1438. }
  1439. /* A weak check on whether the section file positions were
  1440. reasonable. */
  1441. if (bfd_tell (abfd) > nlm_fixed_header (abfd)->codeImageOffset)
  1442. {
  1443. bfd_set_error (bfd_error_invalid_operation);
  1444. goto error_return;
  1445. }
  1446. /* Advance to the relocs. */
  1447. if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
  1448. SEEK_SET) != 0)
  1449. goto error_return;
  1450. /* The format of the relocation entries is dependent upon the
  1451. particular target. We use an external routine to write the reloc
  1452. out. */
  1453. write_import_func = nlm_write_import_func (abfd);
  1454. /* Write out the internal relocation fixups. While we're looping
  1455. over the relocs, we also count the external relocs, which is
  1456. needed when they are written out below. */
  1457. internal_reloc_count = 0;
  1458. external_reloc_count = 0;
  1459. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  1460. {
  1461. arelent **rel_ptr_ptr, **rel_end;
  1462. if (sec->reloc_count == 0)
  1463. continue;
  1464. /* We can only represent relocs within a code or data
  1465. section. We ignore them for a debugging section. */
  1466. if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
  1467. continue;
  1468. /* We need to know how to write out imports */
  1469. if (write_import_func == NULL)
  1470. {
  1471. bfd_set_error (bfd_error_invalid_operation);
  1472. goto error_return;
  1473. }
  1474. rel_ptr_ptr = sec->orelocation;
  1475. rel_end = rel_ptr_ptr + sec->reloc_count;
  1476. for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
  1477. {
  1478. arelent *rel;
  1479. asymbol *sym;
  1480. rel = *rel_ptr_ptr;
  1481. sym = *rel->sym_ptr_ptr;
  1482. if (! bfd_is_und_section (bfd_get_section (sym)))
  1483. {
  1484. ++internal_reloc_count;
  1485. if (! (*write_import_func) (abfd, sec, rel))
  1486. goto error_return;
  1487. }
  1488. else
  1489. ++external_reloc_count;
  1490. }
  1491. }
  1492. nlm_fixed_header (abfd)->numberOfRelocationFixups = internal_reloc_count;
  1493. /* Write out the imports (relocs against external symbols). These
  1494. are output as a symbol name followed by all the relocs for that
  1495. symbol, so we must first gather together all the relocs against
  1496. external symbols and sort them. */
  1497. amt = external_reloc_count * sizeof (struct reloc_and_sec);
  1498. external_relocs = bfd_alloc (abfd, amt);
  1499. if (external_relocs == NULL)
  1500. goto error_return;
  1501. i = 0;
  1502. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  1503. {
  1504. arelent **rel_ptr_ptr, **rel_end;
  1505. if (sec->reloc_count == 0)
  1506. continue;
  1507. rel_ptr_ptr = sec->orelocation;
  1508. rel_end = rel_ptr_ptr + sec->reloc_count;
  1509. for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
  1510. {
  1511. arelent *rel;
  1512. asymbol *sym;
  1513. rel = *rel_ptr_ptr;
  1514. sym = *rel->sym_ptr_ptr;
  1515. if (! bfd_is_und_section (bfd_get_section (sym)))
  1516. continue;
  1517. external_relocs[i].rel = rel;
  1518. external_relocs[i].sec = sec;
  1519. ++i;
  1520. }
  1521. }
  1522. BFD_ASSERT (i == external_reloc_count);
  1523. /* Sort the external relocs by name. */
  1524. qsort (external_relocs, (size_t) external_reloc_count,
  1525. sizeof (struct reloc_and_sec), nlm_external_reloc_compare);
  1526. /* Write out the external relocs. */
  1527. nlm_fixed_header (abfd)->externalReferencesOffset = bfd_tell (abfd);
  1528. c = 0;
  1529. i = 0;
  1530. while (i < external_reloc_count)
  1531. {
  1532. arelent *rel;
  1533. asymbol *sym;
  1534. bfd_size_type j, cnt;
  1535. ++c;
  1536. rel = external_relocs[i].rel;
  1537. sym = *rel->sym_ptr_ptr;
  1538. cnt = 0;
  1539. for (j = i;
  1540. (j < external_reloc_count
  1541. && *external_relocs[j].rel->sym_ptr_ptr == sym);
  1542. j++)
  1543. ++cnt;
  1544. if (! (*nlm_write_external_func (abfd)) (abfd, cnt, sym,
  1545. &external_relocs[i]))
  1546. goto error_return;
  1547. i += cnt;
  1548. }
  1549. nlm_fixed_header (abfd)->numberOfExternalReferences = c;
  1550. /* Write out the public symbols (exports). */
  1551. sym_ptr_ptr = bfd_get_outsymbols (abfd);
  1552. if (sym_ptr_ptr != NULL)
  1553. {
  1554. bfd_vma (*get_public_offset_func) (bfd *, asymbol *);
  1555. bfd_boolean (*write_export_func) (bfd *, asymbol *, bfd_vma);
  1556. asymbol **sym_end;
  1557. nlm_fixed_header (abfd)->publicsOffset = bfd_tell (abfd);
  1558. get_public_offset_func = nlm_get_public_offset_func (abfd);
  1559. write_export_func = nlm_write_export_func (abfd);
  1560. c = 0;
  1561. sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
  1562. for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
  1563. {
  1564. asymbol *sym;
  1565. bfd_byte len;
  1566. bfd_vma offset;
  1567. bfd_byte temp[NLM_TARGET_LONG_SIZE];
  1568. sym = *sym_ptr_ptr;
  1569. if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) == 0
  1570. || bfd_is_und_section (bfd_get_section (sym)))
  1571. continue;
  1572. ++c;
  1573. if (get_public_offset_func)
  1574. {
  1575. /* Most backends can use the code below, but
  1576. unfortunately some use a different scheme. */
  1577. offset = (*get_public_offset_func) (abfd, sym);
  1578. }
  1579. else
  1580. {
  1581. offset = bfd_asymbol_value (sym);
  1582. sec = sym->section;
  1583. if (sec->flags & SEC_CODE)
  1584. {
  1585. offset -= nlm_get_text_low (abfd);
  1586. offset |= NLM_HIBIT;
  1587. }
  1588. else if (sec->flags & (SEC_DATA | SEC_ALLOC))
  1589. {
  1590. /* SEC_ALLOC is for the .bss section. */
  1591. offset -= nlm_get_data_low (abfd);
  1592. }
  1593. else
  1594. {
  1595. /* We can't handle an exported symbol that is not in
  1596. the code or data segment. */
  1597. bfd_set_error (bfd_error_invalid_operation);
  1598. goto error_return;
  1599. }
  1600. }
  1601. if (write_export_func)
  1602. {
  1603. if (! (*write_export_func) (abfd, sym, offset))
  1604. goto error_return;
  1605. }
  1606. else
  1607. {
  1608. len = strlen (sym->name);
  1609. if ((bfd_bwrite (&len, (bfd_size_type) sizeof (bfd_byte), abfd)
  1610. != sizeof (bfd_byte))
  1611. || bfd_bwrite (sym->name, (bfd_size_type) len, abfd) != len)
  1612. goto error_return;
  1613. put_word (abfd, offset, temp);
  1614. if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd)
  1615. != sizeof (temp))
  1616. goto error_return;
  1617. }
  1618. }
  1619. nlm_fixed_header (abfd)->numberOfPublics = c;
  1620. /* Write out the debugging records. The NLM conversion program
  1621. wants to be able to inhibit this, so as a special hack if
  1622. debugInfoOffset is set to -1 we don't write any debugging
  1623. information. This can not be handled by fiddling with the
  1624. symbol table, because exported symbols appear in both the
  1625. exported symbol list and the debugging information. */
  1626. if (nlm_fixed_header (abfd)->debugInfoOffset == (file_ptr) - 1)
  1627. {
  1628. nlm_fixed_header (abfd)->debugInfoOffset = 0;
  1629. nlm_fixed_header (abfd)->numberOfDebugRecords = 0;
  1630. }
  1631. else
  1632. {
  1633. nlm_fixed_header (abfd)->debugInfoOffset = bfd_tell (abfd);
  1634. c = 0;
  1635. sym_ptr_ptr = bfd_get_outsymbols (abfd);
  1636. sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
  1637. for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
  1638. {
  1639. asymbol *sym;
  1640. bfd_byte type, len;
  1641. bfd_vma offset;
  1642. bfd_byte temp[NLM_TARGET_LONG_SIZE];
  1643. sym = *sym_ptr_ptr;
  1644. /* The NLM notion of a debugging symbol is actually what
  1645. BFD calls a local or global symbol. What BFD calls a
  1646. debugging symbol NLM does not understand at all. */
  1647. if ((sym->flags & (BSF_LOCAL | BSF_GLOBAL | BSF_EXPORT)) == 0
  1648. || (sym->flags & BSF_DEBUGGING) != 0
  1649. || bfd_is_und_section (bfd_get_section (sym)))
  1650. continue;
  1651. ++c;
  1652. offset = bfd_asymbol_value (sym);
  1653. sec = sym->section;
  1654. if (sec->flags & SEC_CODE)
  1655. {
  1656. offset -= nlm_get_text_low (abfd);
  1657. type = 1;
  1658. }
  1659. else if (sec->flags & (SEC_DATA | SEC_ALLOC))
  1660. {
  1661. /* SEC_ALLOC is for the .bss section. */
  1662. offset -= nlm_get_data_low (abfd);
  1663. type = 0;
  1664. }
  1665. else
  1666. type = 2;
  1667. /* The type is 0 for data, 1 for code, 2 for absolute. */
  1668. if (bfd_bwrite (&type, (bfd_size_type) sizeof (bfd_byte), abfd)
  1669. != sizeof (bfd_byte))
  1670. goto error_return;
  1671. put_word (abfd, offset, temp);
  1672. if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd)
  1673. != sizeof (temp))
  1674. goto error_return;
  1675. len = strlen (sym->name);
  1676. if ((bfd_bwrite (&len, (bfd_size_type) sizeof (bfd_byte), abfd)
  1677. != sizeof (bfd_byte))
  1678. || bfd_bwrite (sym->name, (bfd_size_type) len, abfd) != len)
  1679. goto error_return;
  1680. }
  1681. nlm_fixed_header (abfd)->numberOfDebugRecords = c;
  1682. }
  1683. }
  1684. /* NLMLINK fills in offset values even if there is no data, so we do
  1685. the same. */
  1686. last = bfd_tell (abfd);
  1687. if (nlm_fixed_header (abfd)->codeImageOffset == 0)
  1688. nlm_fixed_header (abfd)->codeImageOffset = last;
  1689. if (nlm_fixed_header (abfd)->dataImageOffset == 0)
  1690. nlm_fixed_header (abfd)->dataImageOffset = last;
  1691. if (nlm_fixed_header (abfd)->customDataOffset == 0)
  1692. nlm_fixed_header (abfd)->customDataOffset = last;
  1693. if (nlm_fixed_header (abfd)->moduleDependencyOffset == 0)
  1694. nlm_fixed_header (abfd)->moduleDependencyOffset = last;
  1695. if (nlm_fixed_header (abfd)->relocationFixupOffset == 0)
  1696. nlm_fixed_header (abfd)->relocationFixupOffset = last;
  1697. if (nlm_fixed_header (abfd)->externalReferencesOffset == 0)
  1698. nlm_fixed_header (abfd)->externalReferencesOffset = last;
  1699. if (nlm_fixed_header (abfd)->publicsOffset == 0)
  1700. nlm_fixed_header (abfd)->publicsOffset = last;
  1701. if (nlm_fixed_header (abfd)->debugInfoOffset == 0)
  1702. nlm_fixed_header (abfd)->debugInfoOffset = last;
  1703. /* At this point everything has been written out except the fixed
  1704. header. */
  1705. memcpy (nlm_fixed_header (abfd)->signature, nlm_signature (abfd),
  1706. NLM_SIGNATURE_SIZE);
  1707. nlm_fixed_header (abfd)->version = NLM_HEADER_VERSION;
  1708. nlm_fixed_header (abfd)->codeStartOffset =
  1709. (bfd_get_start_address (abfd)
  1710. - nlm_get_text_low (abfd));
  1711. /* We have no convenient way for the caller to pass in the exit
  1712. procedure or the check unload procedure, so the caller must set
  1713. the values in the header to the values of the symbols. */
  1714. nlm_fixed_header (abfd)->exitProcedureOffset -= nlm_get_text_low (abfd);
  1715. if (nlm_fixed_header (abfd)->checkUnloadProcedureOffset != 0)
  1716. nlm_fixed_header (abfd)->checkUnloadProcedureOffset -=
  1717. nlm_get_text_low (abfd);
  1718. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  1719. goto error_return;
  1720. write_prefix_func = nlm_write_prefix_func (abfd);
  1721. if (write_prefix_func)
  1722. {
  1723. if (! (*write_prefix_func) (abfd))
  1724. goto error_return;
  1725. }
  1726. BFD_ASSERT ((bfd_size_type) bfd_tell (abfd)
  1727. == nlm_optional_prefix_size (abfd));
  1728. nlm_swap_fixed_header_out (abfd, nlm_fixed_header (abfd), fixed_header);
  1729. if (bfd_bwrite (fixed_header, nlm_fixed_header_size (abfd), abfd)
  1730. != nlm_fixed_header_size (abfd))
  1731. goto error_return;
  1732. if (fixed_header != NULL)
  1733. free (fixed_header);
  1734. return TRUE;
  1735. error_return:
  1736. if (fixed_header != NULL)
  1737. free (fixed_header);
  1738. return FALSE;
  1739. }