nlmconv.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. /* nlmconv.c -- NLM conversion program
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /* Written by Ian Lance Taylor <ian@cygnus.com>.
  17. This program can be used to convert any appropriate object file
  18. into a NetWare Loadable Module (an NLM). It will accept a linker
  19. specification file which is identical to that accepted by the
  20. NetWare linker, NLMLINK. */
  21. /* AIX requires this to be the first thing in the file. */
  22. #ifndef __GNUC__
  23. # ifdef _AIX
  24. #pragma alloca
  25. #endif
  26. #endif
  27. #include "sysdep.h"
  28. #include "bfd.h"
  29. #include "libiberty.h"
  30. #include "filenames.h"
  31. #include "safe-ctype.h"
  32. #include "ansidecl.h"
  33. #include <time.h>
  34. #include <assert.h>
  35. #include "getopt.h"
  36. /* Internal BFD NLM header. */
  37. #include "libnlm.h"
  38. #include "nlmconv.h"
  39. #ifdef NLMCONV_ALPHA
  40. #include "coff/sym.h"
  41. #include "coff/ecoff.h"
  42. #endif
  43. #include "bucomm.h"
  44. /* If strerror is just a macro, we want to use the one from libiberty
  45. since it will handle undefined values. */
  46. #undef strerror
  47. extern char *strerror (int);
  48. #ifndef SEEK_SET
  49. #define SEEK_SET 0
  50. #endif
  51. #ifndef R_OK
  52. #define R_OK 4
  53. #define W_OK 2
  54. #define X_OK 1
  55. #endif
  56. /* Global variables. */
  57. /* The name used to invoke the program. */
  58. char *program_name;
  59. /* Local variables. */
  60. /* Whether to print out debugging information (currently just controls
  61. whether it prints the linker command if there is one). */
  62. static int debug;
  63. /* The symbol table. */
  64. static asymbol **symbols;
  65. /* A section we create in the output file to hold pointers to where
  66. the sections of the input file end up. We will put a pointer to
  67. this section in the NLM header. These is an entry for each input
  68. section. The format is
  69. null terminated section name
  70. zeroes to adjust to 4 byte boundary
  71. 4 byte section data file pointer
  72. 4 byte section size
  73. We don't need a version number. The way we find this information
  74. is by finding a stamp in the NLM header information. If we need to
  75. change the format of this information, we can simply change the
  76. stamp. */
  77. static asection *secsec;
  78. /* A temporary file name to be unlinked on exit. Actually, for most
  79. errors, we leave it around. It's not clear whether that is helpful
  80. or not. */
  81. static char *unlink_on_exit;
  82. /* The list of long options. */
  83. static struct option long_options[] =
  84. {
  85. { "debug", no_argument, 0, 'd' },
  86. { "header-file", required_argument, 0, 'T' },
  87. { "help", no_argument, 0, 'h' },
  88. { "input-target", required_argument, 0, 'I' },
  89. { "input-format", required_argument, 0, 'I' }, /* Obsolete */
  90. { "linker", required_argument, 0, 'l' },
  91. { "output-target", required_argument, 0, 'O' },
  92. { "output-format", required_argument, 0, 'O' }, /* Obsolete */
  93. { "version", no_argument, 0, 'V' },
  94. { NULL, no_argument, 0, 0 }
  95. };
  96. /* Local routines. */
  97. int main (int, char **);
  98. static void show_usage (FILE *, int);
  99. static const char *select_output_format
  100. (enum bfd_architecture, unsigned long, bfd_boolean);
  101. static void setup_sections (bfd *, asection *, void *);
  102. static void copy_sections (bfd *, asection *, void *);
  103. static void mangle_relocs
  104. (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
  105. static void default_mangle_relocs
  106. (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
  107. static char *link_inputs (struct string_list *, char *, char *);
  108. #ifdef NLMCONV_I386
  109. static void i386_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
  110. #endif
  111. #ifdef NLMCONV_ALPHA
  112. static void alpha_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
  113. #endif
  114. #ifdef NLMCONV_POWERPC
  115. static void powerpc_build_stubs (bfd *, bfd *, asymbol ***, long *);
  116. static void powerpc_resolve_stubs (bfd *, bfd *);
  117. static void powerpc_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
  118. #endif
  119. /* The main routine. */
  120. int
  121. main (int argc, char **argv)
  122. {
  123. int opt;
  124. char *input_file = NULL;
  125. const char *input_format = NULL;
  126. const char *output_format = NULL;
  127. const char *header_file = NULL;
  128. char *ld_arg = NULL;
  129. Nlm_Internal_Fixed_Header fixed_hdr_struct;
  130. Nlm_Internal_Variable_Header var_hdr_struct;
  131. Nlm_Internal_Version_Header version_hdr_struct;
  132. Nlm_Internal_Copyright_Header copyright_hdr_struct;
  133. Nlm_Internal_Extended_Header extended_hdr_struct;
  134. bfd *inbfd;
  135. bfd *outbfd;
  136. asymbol **newsyms, **outsyms;
  137. long symcount, newsymalloc, newsymcount;
  138. long symsize;
  139. asection *text_sec, *bss_sec, *data_sec;
  140. bfd_vma vma;
  141. bfd_size_type align;
  142. asymbol *endsym;
  143. long i;
  144. char inlead, outlead;
  145. bfd_boolean gotstart, gotexit, gotcheck;
  146. struct stat st;
  147. FILE *custom_data = NULL;
  148. FILE *help_data = NULL;
  149. FILE *message_data = NULL;
  150. FILE *rpc_data = NULL;
  151. FILE *shared_data = NULL;
  152. size_t custom_size = 0;
  153. size_t help_size = 0;
  154. size_t message_size = 0;
  155. size_t module_size = 0;
  156. size_t rpc_size = 0;
  157. asection *custom_section = NULL;
  158. asection *help_section = NULL;
  159. asection *message_section = NULL;
  160. asection *module_section = NULL;
  161. asection *rpc_section = NULL;
  162. asection *shared_section = NULL;
  163. bfd *sharedbfd;
  164. size_t shared_offset = 0;
  165. size_t shared_size = 0;
  166. static Nlm_Internal_Fixed_Header sharedhdr;
  167. int len;
  168. char *modname;
  169. char **matching;
  170. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  171. setlocale (LC_MESSAGES, "");
  172. #endif
  173. #if defined (HAVE_SETLOCALE)
  174. setlocale (LC_CTYPE, "");
  175. #endif
  176. bindtextdomain (PACKAGE, LOCALEDIR);
  177. textdomain (PACKAGE);
  178. program_name = argv[0];
  179. xmalloc_set_program_name (program_name);
  180. bfd_set_error_program_name (program_name);
  181. expandargv (&argc, &argv);
  182. bfd_init ();
  183. set_default_bfd_target ();
  184. while ((opt = getopt_long (argc, argv, "dHhI:l:O:T:Vv", long_options,
  185. (int *) NULL))
  186. != EOF)
  187. {
  188. switch (opt)
  189. {
  190. case 'd':
  191. debug = 1;
  192. break;
  193. case 'H':
  194. case 'h':
  195. show_usage (stdout, 0);
  196. break;
  197. case 'I':
  198. input_format = optarg;
  199. break;
  200. case 'l':
  201. ld_arg = optarg;
  202. break;
  203. case 'O':
  204. output_format = optarg;
  205. break;
  206. case 'T':
  207. header_file = optarg;
  208. break;
  209. case 'v':
  210. case 'V':
  211. print_version ("nlmconv");
  212. break;
  213. case 0:
  214. break;
  215. default:
  216. show_usage (stderr, 1);
  217. break;
  218. }
  219. }
  220. /* The input and output files may be named on the command line. */
  221. output_file = NULL;
  222. if (optind < argc)
  223. {
  224. input_file = argv[optind];
  225. ++optind;
  226. if (optind < argc)
  227. {
  228. output_file = argv[optind];
  229. ++optind;
  230. if (optind < argc)
  231. show_usage (stderr, 1);
  232. if (filename_cmp (input_file, output_file) == 0)
  233. {
  234. fatal (_("input and output files must be different"));
  235. }
  236. }
  237. }
  238. /* Initialize the header information to default values. */
  239. fixed_hdr = &fixed_hdr_struct;
  240. memset ((void *) &fixed_hdr_struct, 0, sizeof fixed_hdr_struct);
  241. var_hdr = &var_hdr_struct;
  242. memset ((void *) &var_hdr_struct, 0, sizeof var_hdr_struct);
  243. version_hdr = &version_hdr_struct;
  244. memset ((void *) &version_hdr_struct, 0, sizeof version_hdr_struct);
  245. copyright_hdr = &copyright_hdr_struct;
  246. memset ((void *) &copyright_hdr_struct, 0, sizeof copyright_hdr_struct);
  247. extended_hdr = &extended_hdr_struct;
  248. memset ((void *) &extended_hdr_struct, 0, sizeof extended_hdr_struct);
  249. check_procedure = NULL;
  250. custom_file = NULL;
  251. debug_info = FALSE;
  252. exit_procedure = "_Stop";
  253. export_symbols = NULL;
  254. map_file = NULL;
  255. full_map = FALSE;
  256. help_file = NULL;
  257. import_symbols = NULL;
  258. message_file = NULL;
  259. modules = NULL;
  260. sharelib_file = NULL;
  261. start_procedure = "_Prelude";
  262. verbose = FALSE;
  263. rpc_file = NULL;
  264. parse_errors = 0;
  265. /* Parse the header file (if there is one). */
  266. if (header_file != NULL)
  267. {
  268. if (! nlmlex_file (header_file)
  269. || yyparse () != 0
  270. || parse_errors != 0)
  271. exit (1);
  272. }
  273. if (input_files != NULL)
  274. {
  275. if (input_file != NULL)
  276. {
  277. fatal (_("input file named both on command line and with INPUT"));
  278. }
  279. if (input_files->next == NULL)
  280. input_file = input_files->string;
  281. else
  282. input_file = link_inputs (input_files, ld_arg, map_file);
  283. }
  284. else if (input_file == NULL)
  285. {
  286. non_fatal (_("no input file"));
  287. show_usage (stderr, 1);
  288. }
  289. inbfd = bfd_openr (input_file, input_format);
  290. if (inbfd == NULL)
  291. bfd_fatal (input_file);
  292. if (! bfd_check_format_matches (inbfd, bfd_object, &matching))
  293. {
  294. bfd_nonfatal (input_file);
  295. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  296. {
  297. list_matching_formats (matching);
  298. free (matching);
  299. }
  300. exit (1);
  301. }
  302. if (output_format == NULL)
  303. output_format = select_output_format (bfd_get_arch (inbfd),
  304. bfd_get_mach (inbfd),
  305. bfd_big_endian (inbfd));
  306. assert (output_format != NULL);
  307. /* Use the output file named on the command line if it exists.
  308. Otherwise use the file named in the OUTPUT statement. */
  309. if (output_file == NULL)
  310. {
  311. non_fatal (_("no name for output file"));
  312. show_usage (stderr, 1);
  313. }
  314. outbfd = bfd_openw (output_file, output_format);
  315. if (outbfd == NULL)
  316. bfd_fatal (output_file);
  317. if (! bfd_set_format (outbfd, bfd_object))
  318. bfd_fatal (output_file);
  319. assert (bfd_get_flavour (outbfd) == bfd_target_nlm_flavour);
  320. /* XXX: Should we accept the unknown bfd format here ? */
  321. if (bfd_arch_get_compatible (inbfd, outbfd, TRUE) == NULL)
  322. non_fatal (_("warning: input and output formats are not compatible"));
  323. /* Move the values read from the command file into outbfd. */
  324. *nlm_fixed_header (outbfd) = fixed_hdr_struct;
  325. *nlm_variable_header (outbfd) = var_hdr_struct;
  326. *nlm_version_header (outbfd) = version_hdr_struct;
  327. *nlm_copyright_header (outbfd) = copyright_hdr_struct;
  328. *nlm_extended_header (outbfd) = extended_hdr_struct;
  329. /* Start copying the input BFD to the output BFD. */
  330. if (! bfd_set_file_flags (outbfd, bfd_get_file_flags (inbfd)))
  331. bfd_fatal (bfd_get_filename (outbfd));
  332. symsize = bfd_get_symtab_upper_bound (inbfd);
  333. if (symsize < 0)
  334. bfd_fatal (input_file);
  335. symbols = (asymbol **) xmalloc (symsize);
  336. symcount = bfd_canonicalize_symtab (inbfd, symbols);
  337. if (symcount < 0)
  338. bfd_fatal (input_file);
  339. /* Make sure we have a .bss section. */
  340. bss_sec = bfd_get_section_by_name (outbfd, NLM_UNINITIALIZED_DATA_NAME);
  341. if (bss_sec == NULL)
  342. {
  343. bss_sec = bfd_make_section_with_flags (outbfd,
  344. NLM_UNINITIALIZED_DATA_NAME,
  345. SEC_ALLOC);
  346. if (bss_sec == NULL
  347. || ! bfd_set_section_alignment (outbfd, bss_sec, 1))
  348. bfd_fatal (_("make .bss section"));
  349. }
  350. /* We store the original section names in the .nlmsections section,
  351. so that programs which understand it can resurrect the original
  352. sections from the NLM. We will put a pointer to .nlmsections in
  353. the NLM header area. */
  354. secsec = bfd_make_section_with_flags (outbfd, ".nlmsections",
  355. SEC_HAS_CONTENTS);
  356. if (secsec == NULL)
  357. bfd_fatal (_("make .nlmsections section"));
  358. #ifdef NLMCONV_POWERPC
  359. /* For PowerPC NetWare we need to build stubs for calls to undefined
  360. symbols. Because each stub requires an entry in the TOC section
  361. which must be at the same location as other entries in the TOC
  362. section, we must do this before determining where the TOC section
  363. goes in setup_sections. */
  364. if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
  365. powerpc_build_stubs (inbfd, outbfd, &symbols, &symcount);
  366. #endif
  367. /* Set up the sections. */
  368. bfd_map_over_sections (inbfd, setup_sections, (void *) outbfd);
  369. text_sec = bfd_get_section_by_name (outbfd, NLM_CODE_NAME);
  370. /* The .bss section immediately follows the .data section. */
  371. data_sec = bfd_get_section_by_name (outbfd, NLM_INITIALIZED_DATA_NAME);
  372. if (data_sec != NULL)
  373. {
  374. bfd_size_type add;
  375. vma = bfd_get_section_size (data_sec);
  376. align = 1 << bss_sec->alignment_power;
  377. add = ((vma + align - 1) &~ (align - 1)) - vma;
  378. vma += add;
  379. if (! bfd_set_section_vma (outbfd, bss_sec, vma))
  380. bfd_fatal (_("set .bss vma"));
  381. if (add != 0)
  382. {
  383. bfd_size_type data_size;
  384. data_size = bfd_get_section_size (data_sec);
  385. if (! bfd_set_section_size (outbfd, data_sec, data_size + add))
  386. bfd_fatal (_("set .data size"));
  387. }
  388. }
  389. /* Adjust symbol information. */
  390. inlead = bfd_get_symbol_leading_char (inbfd);
  391. outlead = bfd_get_symbol_leading_char (outbfd);
  392. gotstart = FALSE;
  393. gotexit = FALSE;
  394. gotcheck = FALSE;
  395. newsymalloc = 10;
  396. newsyms = (asymbol **) xmalloc (newsymalloc * sizeof (asymbol *));
  397. newsymcount = 0;
  398. endsym = NULL;
  399. for (i = 0; i < symcount; i++)
  400. {
  401. asymbol *sym;
  402. sym = symbols[i];
  403. /* Add or remove a leading underscore. */
  404. if (inlead != outlead)
  405. {
  406. if (inlead != '\0')
  407. {
  408. if (bfd_asymbol_name (sym)[0] == inlead)
  409. {
  410. if (outlead == '\0')
  411. ++sym->name;
  412. else
  413. {
  414. char *new_name;
  415. new_name = xmalloc (strlen (bfd_asymbol_name (sym)) + 1);
  416. new_name[0] = outlead;
  417. strcpy (new_name + 1, bfd_asymbol_name (sym) + 1);
  418. sym->name = new_name;
  419. }
  420. }
  421. }
  422. else
  423. {
  424. char *new_name;
  425. new_name = xmalloc (strlen (bfd_asymbol_name (sym)) + 2);
  426. new_name[0] = outlead;
  427. strcpy (new_name + 1, bfd_asymbol_name (sym));
  428. sym->name = new_name;
  429. }
  430. }
  431. /* NLM's have an uninitialized data section, but they do not
  432. have a common section in the Unix sense. Move all common
  433. symbols into the .bss section, and mark them as exported. */
  434. if (bfd_is_com_section (bfd_get_section (sym)))
  435. {
  436. bfd_vma size = sym->value;
  437. sym->section = bss_sec;
  438. sym->value = bfd_get_section_size (bss_sec);
  439. size += sym->value;
  440. align = 1 << bss_sec->alignment_power;
  441. size = (size + align - 1) & ~(align - 1);
  442. bfd_set_section_size (outbfd, bss_sec, size);
  443. sym->flags |= BSF_EXPORT | BSF_GLOBAL;
  444. }
  445. else if (bfd_get_section (sym)->output_section != NULL)
  446. {
  447. /* Move the symbol into the output section. */
  448. sym->value += bfd_get_section (sym)->output_offset;
  449. sym->section = bfd_get_section (sym)->output_section;
  450. /* This is no longer a section symbol. */
  451. sym->flags &=~ BSF_SECTION_SYM;
  452. }
  453. /* Force _edata and _end to be defined. This would normally be
  454. done by the linker, but the manipulation of the common
  455. symbols will confuse it. */
  456. if ((sym->flags & BSF_DEBUGGING) == 0
  457. && bfd_asymbol_name (sym)[0] == '_'
  458. && bfd_is_und_section (bfd_get_section (sym)))
  459. {
  460. if (strcmp (bfd_asymbol_name (sym), "_edata") == 0)
  461. {
  462. sym->section = bss_sec;
  463. sym->value = 0;
  464. }
  465. if (strcmp (bfd_asymbol_name (sym), "_end") == 0)
  466. {
  467. sym->section = bss_sec;
  468. endsym = sym;
  469. }
  470. #ifdef NLMCONV_POWERPC
  471. /* For PowerPC NetWare, we define __GOT0. This is the start
  472. of the .got section. */
  473. if (bfd_get_arch (inbfd) == bfd_arch_powerpc
  474. && strcmp (bfd_asymbol_name (sym), "__GOT0") == 0)
  475. {
  476. asection *got_sec;
  477. got_sec = bfd_get_section_by_name (inbfd, ".got");
  478. assert (got_sec != (asection *) NULL);
  479. sym->value = got_sec->output_offset;
  480. sym->section = got_sec->output_section;
  481. }
  482. #endif
  483. }
  484. /* If this is a global symbol, check the export list. */
  485. if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) != 0)
  486. {
  487. struct string_list *l;
  488. int found_simple;
  489. /* Unfortunately, a symbol can appear multiple times on the
  490. export list, with and without prefixes. */
  491. found_simple = 0;
  492. for (l = export_symbols; l != NULL; l = l->next)
  493. {
  494. if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
  495. found_simple = 1;
  496. else
  497. {
  498. char *zbase;
  499. zbase = strchr (l->string, '@');
  500. if (zbase != NULL
  501. && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
  502. {
  503. /* We must add a symbol with this prefix. */
  504. if (newsymcount >= newsymalloc)
  505. {
  506. newsymalloc += 10;
  507. newsyms = ((asymbol **)
  508. xrealloc ((void *) newsyms,
  509. (newsymalloc
  510. * sizeof (asymbol *))));
  511. }
  512. newsyms[newsymcount] =
  513. (asymbol *) xmalloc (sizeof (asymbol));
  514. *newsyms[newsymcount] = *sym;
  515. newsyms[newsymcount]->name = l->string;
  516. ++newsymcount;
  517. }
  518. }
  519. }
  520. if (! found_simple)
  521. {
  522. /* The unmodified symbol is actually not exported at
  523. all. */
  524. sym->flags &=~ (BSF_GLOBAL | BSF_EXPORT);
  525. sym->flags |= BSF_LOCAL;
  526. }
  527. }
  528. /* If it's an undefined symbol, see if it's on the import list.
  529. Change the prefix if necessary. */
  530. if (bfd_is_und_section (bfd_get_section (sym)))
  531. {
  532. struct string_list *l;
  533. for (l = import_symbols; l != NULL; l = l->next)
  534. {
  535. if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
  536. break;
  537. else
  538. {
  539. char *zbase;
  540. zbase = strchr (l->string, '@');
  541. if (zbase != NULL
  542. && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
  543. {
  544. sym->name = l->string;
  545. break;
  546. }
  547. }
  548. }
  549. if (l == NULL)
  550. non_fatal (_("warning: symbol %s imported but not in import list"),
  551. bfd_asymbol_name (sym));
  552. }
  553. /* See if it's one of the special named symbols. */
  554. if ((sym->flags & BSF_DEBUGGING) == 0)
  555. {
  556. bfd_vma val;
  557. /* FIXME: If these symbols are not in the .text section, we
  558. add the .text section size to the value. This may not be
  559. correct for all targets. I'm not sure how this should
  560. really be handled. */
  561. if (strcmp (bfd_asymbol_name (sym), start_procedure) == 0)
  562. {
  563. val = bfd_asymbol_value (sym);
  564. if (bfd_get_section (sym) == data_sec
  565. && text_sec != (asection *) NULL)
  566. val += bfd_section_size (outbfd, text_sec);
  567. if (! bfd_set_start_address (outbfd, val))
  568. bfd_fatal (_("set start address"));
  569. gotstart = TRUE;
  570. }
  571. if (strcmp (bfd_asymbol_name (sym), exit_procedure) == 0)
  572. {
  573. val = bfd_asymbol_value (sym);
  574. if (bfd_get_section (sym) == data_sec
  575. && text_sec != (asection *) NULL)
  576. val += bfd_section_size (outbfd, text_sec);
  577. nlm_fixed_header (outbfd)->exitProcedureOffset = val;
  578. gotexit = TRUE;
  579. }
  580. if (check_procedure != NULL
  581. && strcmp (bfd_asymbol_name (sym), check_procedure) == 0)
  582. {
  583. val = bfd_asymbol_value (sym);
  584. if (bfd_get_section (sym) == data_sec
  585. && text_sec != (asection *) NULL)
  586. val += bfd_section_size (outbfd, text_sec);
  587. nlm_fixed_header (outbfd)->checkUnloadProcedureOffset = val;
  588. gotcheck = TRUE;
  589. }
  590. }
  591. }
  592. if (endsym != NULL)
  593. {
  594. endsym->value = bfd_get_section_size (bss_sec);
  595. /* FIXME: If any relocs referring to _end use inplace addends,
  596. then I think they need to be updated. This is handled by
  597. i386_mangle_relocs. Is it needed for any other object
  598. formats? */
  599. }
  600. if (newsymcount == 0)
  601. outsyms = symbols;
  602. else
  603. {
  604. outsyms = (asymbol **) xmalloc ((symcount + newsymcount + 1)
  605. * sizeof (asymbol *));
  606. memcpy (outsyms, symbols, symcount * sizeof (asymbol *));
  607. memcpy (outsyms + symcount, newsyms, newsymcount * sizeof (asymbol *));
  608. outsyms[symcount + newsymcount] = NULL;
  609. }
  610. bfd_set_symtab (outbfd, outsyms, symcount + newsymcount);
  611. if (! gotstart)
  612. non_fatal (_("warning: START procedure %s not defined"), start_procedure);
  613. if (! gotexit)
  614. non_fatal (_("warning: EXIT procedure %s not defined"), exit_procedure);
  615. if (check_procedure != NULL && ! gotcheck)
  616. non_fatal (_("warning: CHECK procedure %s not defined"), check_procedure);
  617. /* Add additional sections required for the header information. */
  618. if (custom_file != NULL)
  619. {
  620. custom_data = fopen (custom_file, "r");
  621. if (custom_data == NULL
  622. || fstat (fileno (custom_data), &st) < 0)
  623. {
  624. fprintf (stderr, "%s:%s: %s\n", program_name, custom_file,
  625. strerror (errno));
  626. custom_file = NULL;
  627. }
  628. else
  629. {
  630. custom_size = st.st_size;
  631. custom_section = bfd_make_section_with_flags (outbfd, ".nlmcustom",
  632. SEC_HAS_CONTENTS);
  633. if (custom_section == NULL
  634. || ! bfd_set_section_size (outbfd, custom_section, custom_size))
  635. bfd_fatal (_("custom section"));
  636. }
  637. }
  638. if (help_file != NULL)
  639. {
  640. help_data = fopen (help_file, "r");
  641. if (help_data == NULL
  642. || fstat (fileno (help_data), &st) < 0)
  643. {
  644. fprintf (stderr, "%s:%s: %s\n", program_name, help_file,
  645. strerror (errno));
  646. help_file = NULL;
  647. }
  648. else
  649. {
  650. help_size = st.st_size;
  651. help_section = bfd_make_section_with_flags (outbfd, ".nlmhelp",
  652. SEC_HAS_CONTENTS);
  653. if (help_section == NULL
  654. || ! bfd_set_section_size (outbfd, help_section, help_size))
  655. bfd_fatal (_("help section"));
  656. LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
  657. }
  658. }
  659. if (message_file != NULL)
  660. {
  661. message_data = fopen (message_file, "r");
  662. if (message_data == NULL
  663. || fstat (fileno (message_data), &st) < 0)
  664. {
  665. fprintf (stderr, "%s:%s: %s\n", program_name, message_file,
  666. strerror (errno));
  667. message_file = NULL;
  668. }
  669. else
  670. {
  671. message_size = st.st_size;
  672. message_section = bfd_make_section_with_flags (outbfd,
  673. ".nlmmessages",
  674. SEC_HAS_CONTENTS);
  675. if (message_section == NULL
  676. || ! bfd_set_section_size (outbfd, message_section, message_size))
  677. bfd_fatal (_("message section"));
  678. LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
  679. }
  680. }
  681. if (modules != NULL)
  682. {
  683. struct string_list *l;
  684. module_size = 0;
  685. for (l = modules; l != NULL; l = l->next)
  686. module_size += strlen (l->string) + 1;
  687. module_section = bfd_make_section_with_flags (outbfd, ".nlmmodules",
  688. SEC_HAS_CONTENTS);
  689. if (module_section == NULL
  690. || ! bfd_set_section_size (outbfd, module_section, module_size))
  691. bfd_fatal (_("module section"));
  692. }
  693. if (rpc_file != NULL)
  694. {
  695. rpc_data = fopen (rpc_file, "r");
  696. if (rpc_data == NULL
  697. || fstat (fileno (rpc_data), &st) < 0)
  698. {
  699. fprintf (stderr, "%s:%s: %s\n", program_name, rpc_file,
  700. strerror (errno));
  701. rpc_file = NULL;
  702. }
  703. else
  704. {
  705. rpc_size = st.st_size;
  706. rpc_section = bfd_make_section_with_flags (outbfd, ".nlmrpc",
  707. SEC_HAS_CONTENTS);
  708. if (rpc_section == NULL
  709. || ! bfd_set_section_size (outbfd, rpc_section, rpc_size))
  710. bfd_fatal (_("rpc section"));
  711. LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
  712. }
  713. }
  714. if (sharelib_file != NULL)
  715. {
  716. sharedbfd = bfd_openr (sharelib_file, output_format);
  717. if (sharedbfd == NULL
  718. || ! bfd_check_format (sharedbfd, bfd_object))
  719. {
  720. fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
  721. bfd_errmsg (bfd_get_error ()));
  722. sharelib_file = NULL;
  723. }
  724. else
  725. {
  726. sharedhdr = *nlm_fixed_header (sharedbfd);
  727. bfd_close (sharedbfd);
  728. shared_data = fopen (sharelib_file, "r");
  729. if (shared_data == NULL
  730. || (fstat (fileno (shared_data), &st) < 0))
  731. {
  732. fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
  733. strerror (errno));
  734. sharelib_file = NULL;
  735. }
  736. else
  737. {
  738. /* If we were clever, we could just copy out the
  739. sections of the shared library which we actually
  740. need. However, we would have to figure out the sizes
  741. of the external and public information, and that can
  742. not be done without reading through them. */
  743. if (sharedhdr.uninitializedDataSize > 0)
  744. {
  745. /* There is no place to record this information. */
  746. non_fatal (_("%s: warning: shared libraries can not have uninitialized data"),
  747. sharelib_file);
  748. }
  749. shared_offset = st.st_size;
  750. if (shared_offset > (size_t) sharedhdr.codeImageOffset)
  751. shared_offset = sharedhdr.codeImageOffset;
  752. if (shared_offset > (size_t) sharedhdr.dataImageOffset)
  753. shared_offset = sharedhdr.dataImageOffset;
  754. if (shared_offset > (size_t) sharedhdr.relocationFixupOffset)
  755. shared_offset = sharedhdr.relocationFixupOffset;
  756. if (shared_offset > (size_t) sharedhdr.externalReferencesOffset)
  757. shared_offset = sharedhdr.externalReferencesOffset;
  758. if (shared_offset > (size_t) sharedhdr.publicsOffset)
  759. shared_offset = sharedhdr.publicsOffset;
  760. shared_size = st.st_size - shared_offset;
  761. shared_section = bfd_make_section_with_flags (outbfd,
  762. ".nlmshared",
  763. SEC_HAS_CONTENTS);
  764. if (shared_section == NULL
  765. || ! bfd_set_section_size (outbfd, shared_section,
  766. shared_size))
  767. bfd_fatal (_("shared section"));
  768. LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
  769. }
  770. }
  771. }
  772. /* Check whether a version was given. */
  773. if (!CONST_STRNEQ (version_hdr->stamp, "VeRsIoN#"))
  774. non_fatal (_("warning: No version number given"));
  775. /* At least for now, always create an extended header, because that
  776. is what NLMLINK does. */
  777. LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
  778. LITMEMCPY (nlm_cygnus_ext_header (outbfd)->stamp, "CyGnUsEx");
  779. /* If the date was not given, force it in. */
  780. if (nlm_version_header (outbfd)->month == 0
  781. && nlm_version_header (outbfd)->day == 0
  782. && nlm_version_header (outbfd)->year == 0)
  783. {
  784. time_t now;
  785. struct tm *ptm;
  786. time (&now);
  787. ptm = localtime (&now);
  788. nlm_version_header (outbfd)->month = ptm->tm_mon + 1;
  789. nlm_version_header (outbfd)->day = ptm->tm_mday;
  790. nlm_version_header (outbfd)->year = ptm->tm_year + 1900;
  791. LITMEMCPY (version_hdr->stamp, "VeRsIoN#");
  792. }
  793. #ifdef NLMCONV_POWERPC
  794. /* Resolve the stubs we build for PowerPC NetWare. */
  795. if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
  796. powerpc_resolve_stubs (inbfd, outbfd);
  797. #endif
  798. /* Copy over the sections. */
  799. bfd_map_over_sections (inbfd, copy_sections, (void *) outbfd);
  800. /* Finish up the header information. */
  801. if (custom_file != NULL)
  802. {
  803. void *data;
  804. data = xmalloc (custom_size);
  805. if (fread (data, 1, custom_size, custom_data) != custom_size)
  806. non_fatal (_("%s: read: %s"), custom_file, strerror (errno));
  807. else
  808. {
  809. if (! bfd_set_section_contents (outbfd, custom_section, data,
  810. (file_ptr) 0, custom_size))
  811. bfd_fatal (_("custom section"));
  812. nlm_fixed_header (outbfd)->customDataOffset =
  813. custom_section->filepos;
  814. nlm_fixed_header (outbfd)->customDataSize = custom_size;
  815. }
  816. free (data);
  817. }
  818. if (! debug_info)
  819. {
  820. /* As a special hack, the backend recognizes a debugInfoOffset
  821. of -1 to mean that it should not output any debugging
  822. information. This can not be handling by fiddling with the
  823. symbol table because exported symbols appear in both the
  824. export information and the debugging information. */
  825. nlm_fixed_header (outbfd)->debugInfoOffset = (file_ptr) -1;
  826. }
  827. if (full_map)
  828. non_fatal (_("warning: FULLMAP is not supported; try ld -M"));
  829. if (help_file != NULL)
  830. {
  831. void *data;
  832. data = xmalloc (help_size);
  833. if (fread (data, 1, help_size, help_data) != help_size)
  834. non_fatal (_("%s: read: %s"), help_file, strerror (errno));
  835. else
  836. {
  837. if (! bfd_set_section_contents (outbfd, help_section, data,
  838. (file_ptr) 0, help_size))
  839. bfd_fatal (_("help section"));
  840. nlm_extended_header (outbfd)->helpFileOffset =
  841. help_section->filepos;
  842. nlm_extended_header (outbfd)->helpFileLength = help_size;
  843. }
  844. free (data);
  845. }
  846. if (message_file != NULL)
  847. {
  848. void *data;
  849. data = xmalloc (message_size);
  850. if (fread (data, 1, message_size, message_data) != message_size)
  851. non_fatal (_("%s: read: %s"), message_file, strerror (errno));
  852. else
  853. {
  854. if (! bfd_set_section_contents (outbfd, message_section, data,
  855. (file_ptr) 0, message_size))
  856. bfd_fatal (_("message section"));
  857. nlm_extended_header (outbfd)->messageFileOffset =
  858. message_section->filepos;
  859. nlm_extended_header (outbfd)->messageFileLength = message_size;
  860. /* FIXME: Are these offsets correct on all platforms? Are
  861. they 32 bits on all platforms? What endianness? */
  862. nlm_extended_header (outbfd)->languageID =
  863. bfd_h_get_32 (outbfd, (bfd_byte *) data + 106);
  864. nlm_extended_header (outbfd)->messageCount =
  865. bfd_h_get_32 (outbfd, (bfd_byte *) data + 110);
  866. }
  867. free (data);
  868. }
  869. if (modules != NULL)
  870. {
  871. void *data;
  872. unsigned char *set;
  873. struct string_list *l;
  874. bfd_size_type c;
  875. data = xmalloc (module_size);
  876. c = 0;
  877. set = (unsigned char *) data;
  878. for (l = modules; l != NULL; l = l->next)
  879. {
  880. *set = strlen (l->string);
  881. strncpy ((char *) set + 1, l->string, *set);
  882. set += *set + 1;
  883. ++c;
  884. }
  885. if (! bfd_set_section_contents (outbfd, module_section, data,
  886. (file_ptr) 0, module_size))
  887. bfd_fatal (_("module section"));
  888. nlm_fixed_header (outbfd)->moduleDependencyOffset =
  889. module_section->filepos;
  890. nlm_fixed_header (outbfd)->numberOfModuleDependencies = c;
  891. }
  892. if (rpc_file != NULL)
  893. {
  894. void *data;
  895. data = xmalloc (rpc_size);
  896. if (fread (data, 1, rpc_size, rpc_data) != rpc_size)
  897. non_fatal (_("%s: read: %s"), rpc_file, strerror (errno));
  898. else
  899. {
  900. if (! bfd_set_section_contents (outbfd, rpc_section, data,
  901. (file_ptr) 0, rpc_size))
  902. bfd_fatal (_("rpc section"));
  903. nlm_extended_header (outbfd)->RPCDataOffset =
  904. rpc_section->filepos;
  905. nlm_extended_header (outbfd)->RPCDataLength = rpc_size;
  906. }
  907. free (data);
  908. }
  909. if (sharelib_file != NULL)
  910. {
  911. void *data;
  912. data = xmalloc (shared_size);
  913. if (fseek (shared_data, shared_offset, SEEK_SET) != 0
  914. || fread (data, 1, shared_size, shared_data) != shared_size)
  915. non_fatal (_("%s: read: %s"), sharelib_file, strerror (errno));
  916. else
  917. {
  918. if (! bfd_set_section_contents (outbfd, shared_section, data,
  919. (file_ptr) 0, shared_size))
  920. bfd_fatal (_("shared section"));
  921. }
  922. nlm_extended_header (outbfd)->sharedCodeOffset =
  923. sharedhdr.codeImageOffset - shared_offset + shared_section->filepos;
  924. nlm_extended_header (outbfd)->sharedCodeLength =
  925. sharedhdr.codeImageSize;
  926. nlm_extended_header (outbfd)->sharedDataOffset =
  927. sharedhdr.dataImageOffset - shared_offset + shared_section->filepos;
  928. nlm_extended_header (outbfd)->sharedDataLength =
  929. sharedhdr.dataImageSize;
  930. nlm_extended_header (outbfd)->sharedRelocationFixupOffset =
  931. (sharedhdr.relocationFixupOffset
  932. - shared_offset
  933. + shared_section->filepos);
  934. nlm_extended_header (outbfd)->sharedRelocationFixupCount =
  935. sharedhdr.numberOfRelocationFixups;
  936. nlm_extended_header (outbfd)->sharedExternalReferenceOffset =
  937. (sharedhdr.externalReferencesOffset
  938. - shared_offset
  939. + shared_section->filepos);
  940. nlm_extended_header (outbfd)->sharedExternalReferenceCount =
  941. sharedhdr.numberOfExternalReferences;
  942. nlm_extended_header (outbfd)->sharedPublicsOffset =
  943. sharedhdr.publicsOffset - shared_offset + shared_section->filepos;
  944. nlm_extended_header (outbfd)->sharedPublicsCount =
  945. sharedhdr.numberOfPublics;
  946. nlm_extended_header (outbfd)->sharedDebugRecordOffset =
  947. sharedhdr.debugInfoOffset - shared_offset + shared_section->filepos;
  948. nlm_extended_header (outbfd)->sharedDebugRecordCount =
  949. sharedhdr.numberOfDebugRecords;
  950. nlm_extended_header (outbfd)->SharedInitializationOffset =
  951. sharedhdr.codeStartOffset;
  952. nlm_extended_header (outbfd)->SharedExitProcedureOffset =
  953. sharedhdr.exitProcedureOffset;
  954. free (data);
  955. }
  956. {
  957. const int max_len = NLM_MODULE_NAME_SIZE - 2;
  958. const char * filename = lbasename (output_file);
  959. len = strlen (filename);
  960. if (len > max_len)
  961. len = max_len;
  962. nlm_fixed_header (outbfd)->moduleName[0] = len;
  963. strncpy (nlm_fixed_header (outbfd)->moduleName + 1, filename, max_len);
  964. nlm_fixed_header (outbfd)->moduleName[max_len + 1] = '\0';
  965. for (modname = nlm_fixed_header (outbfd)->moduleName;
  966. *modname != '\0';
  967. modname++)
  968. *modname = TOUPPER (*modname);
  969. }
  970. strncpy (nlm_variable_header (outbfd)->oldThreadName, " LONG",
  971. NLM_OLD_THREAD_NAME_LENGTH);
  972. nlm_cygnus_ext_header (outbfd)->offset = secsec->filepos;
  973. nlm_cygnus_ext_header (outbfd)->length = bfd_section_size (outbfd, secsec);
  974. if (! bfd_close (outbfd))
  975. bfd_fatal (output_file);
  976. if (! bfd_close (inbfd))
  977. bfd_fatal (input_file);
  978. if (unlink_on_exit != NULL)
  979. unlink (unlink_on_exit);
  980. return 0;
  981. }
  982. /* Show a usage message and exit. */
  983. static void
  984. show_usage (FILE *file, int status)
  985. {
  986. fprintf (file, _("Usage: %s [option(s)] [in-file [out-file]]\n"), program_name);
  987. fprintf (file, _(" Convert an object file into a NetWare Loadable Module\n"));
  988. fprintf (file, _(" The options are:\n\
  989. -I --input-target=<bfdname> Set the input binary file format\n\
  990. -O --output-target=<bfdname> Set the output binary file format\n\
  991. -T --header-file=<file> Read <file> for NLM header information\n\
  992. -l --linker=<linker> Use <linker> for any linking\n\
  993. -d --debug Display on stderr the linker command line\n\
  994. @<file> Read options from <file>.\n\
  995. -h --help Display this information\n\
  996. -v --version Display the program's version\n\
  997. "));
  998. if (REPORT_BUGS_TO[0] && status == 0)
  999. fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  1000. exit (status);
  1001. }
  1002. /* Select the output format based on the input architecture, machine,
  1003. and endianness. This chooses the appropriate NLM target. */
  1004. static const char *
  1005. select_output_format (enum bfd_architecture arch, unsigned long mach,
  1006. bfd_boolean bigendian ATTRIBUTE_UNUSED)
  1007. {
  1008. switch (arch)
  1009. {
  1010. #ifdef NLMCONV_I386
  1011. case bfd_arch_i386:
  1012. return "nlm32-i386";
  1013. #endif
  1014. #ifdef NLMCONV_SPARC
  1015. case bfd_arch_sparc:
  1016. return "nlm32-sparc";
  1017. #endif
  1018. #ifdef NLMCONV_ALPHA
  1019. case bfd_arch_alpha:
  1020. return "nlm32-alpha";
  1021. #endif
  1022. #ifdef NLMCONV_POWERPC
  1023. case bfd_arch_powerpc:
  1024. return "nlm32-powerpc";
  1025. #endif
  1026. default:
  1027. fatal (_("support not compiled in for %s"),
  1028. bfd_printable_arch_mach (arch, mach));
  1029. }
  1030. /*NOTREACHED*/
  1031. }
  1032. /* The BFD sections are copied in two passes. This function selects
  1033. the output section for each input section, and sets up the section
  1034. name, size, etc. */
  1035. static void
  1036. setup_sections (bfd *inbfd ATTRIBUTE_UNUSED, asection *insec, void *data_ptr)
  1037. {
  1038. bfd *outbfd = (bfd *) data_ptr;
  1039. flagword f;
  1040. const char *outname;
  1041. asection *outsec;
  1042. bfd_vma offset;
  1043. bfd_size_type align;
  1044. bfd_size_type add;
  1045. bfd_size_type secsecsize;
  1046. f = bfd_get_section_flags (inbfd, insec);
  1047. if (f & SEC_CODE)
  1048. outname = NLM_CODE_NAME;
  1049. else if ((f & SEC_LOAD) && (f & SEC_HAS_CONTENTS))
  1050. outname = NLM_INITIALIZED_DATA_NAME;
  1051. else if (f & SEC_ALLOC)
  1052. outname = NLM_UNINITIALIZED_DATA_NAME;
  1053. else
  1054. outname = bfd_section_name (inbfd, insec);
  1055. outsec = bfd_get_section_by_name (outbfd, outname);
  1056. if (outsec == NULL)
  1057. {
  1058. outsec = bfd_make_section (outbfd, outname);
  1059. if (outsec == NULL)
  1060. bfd_fatal (_("make section"));
  1061. }
  1062. insec->output_section = outsec;
  1063. offset = bfd_section_size (outbfd, outsec);
  1064. align = 1 << bfd_section_alignment (inbfd, insec);
  1065. add = ((offset + align - 1) &~ (align - 1)) - offset;
  1066. insec->output_offset = offset + add;
  1067. if (! bfd_set_section_size (outbfd, outsec,
  1068. (bfd_section_size (outbfd, outsec)
  1069. + bfd_section_size (inbfd, insec)
  1070. + add)))
  1071. bfd_fatal (_("set section size"));
  1072. if ((bfd_section_alignment (inbfd, insec)
  1073. > bfd_section_alignment (outbfd, outsec))
  1074. && ! bfd_set_section_alignment (outbfd, outsec,
  1075. bfd_section_alignment (inbfd, insec)))
  1076. bfd_fatal (_("set section alignment"));
  1077. if (! bfd_set_section_flags (outbfd, outsec,
  1078. f | bfd_get_section_flags (outbfd, outsec)))
  1079. bfd_fatal (_("set section flags"));
  1080. bfd_set_reloc (outbfd, outsec, (arelent **) NULL, 0);
  1081. /* For each input section we allocate space for an entry in
  1082. .nlmsections. */
  1083. secsecsize = bfd_section_size (outbfd, secsec);
  1084. secsecsize += strlen (bfd_section_name (inbfd, insec)) + 1;
  1085. secsecsize = (secsecsize + 3) &~ 3;
  1086. secsecsize += 8;
  1087. if (! bfd_set_section_size (outbfd, secsec, secsecsize))
  1088. bfd_fatal (_("set .nlmsections size"));
  1089. }
  1090. /* Copy the section contents. */
  1091. static void
  1092. copy_sections (bfd *inbfd, asection *insec, void *data_ptr)
  1093. {
  1094. static bfd_size_type secsecoff = 0;
  1095. bfd *outbfd = (bfd *) data_ptr;
  1096. const char *inname;
  1097. asection *outsec;
  1098. bfd_size_type size;
  1099. void *contents;
  1100. long reloc_size;
  1101. bfd_byte buf[4];
  1102. bfd_size_type add;
  1103. inname = bfd_section_name (inbfd, insec);
  1104. outsec = insec->output_section;
  1105. assert (outsec != NULL);
  1106. size = bfd_get_section_size (insec);
  1107. if ((bfd_get_section_flags (inbfd, insec) & SEC_HAS_CONTENTS) == 0)
  1108. contents = NULL;
  1109. else
  1110. {
  1111. contents = xmalloc (size);
  1112. if (! bfd_get_section_contents (inbfd, insec, contents,
  1113. (file_ptr) 0, size))
  1114. bfd_fatal (bfd_get_filename (inbfd));
  1115. }
  1116. reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
  1117. if (reloc_size < 0)
  1118. bfd_fatal (bfd_get_filename (inbfd));
  1119. if (reloc_size != 0)
  1120. {
  1121. arelent **relocs;
  1122. long reloc_count;
  1123. relocs = (arelent **) xmalloc (reloc_size);
  1124. reloc_count = bfd_canonicalize_reloc (inbfd, insec, relocs, symbols);
  1125. if (reloc_count < 0)
  1126. bfd_fatal (bfd_get_filename (inbfd));
  1127. mangle_relocs (outbfd, insec, &relocs, &reloc_count, (char *) contents,
  1128. size);
  1129. /* FIXME: refers to internal BFD fields. */
  1130. if (outsec->orelocation != (arelent **) NULL)
  1131. {
  1132. bfd_size_type total_count;
  1133. arelent **combined;
  1134. total_count = reloc_count + outsec->reloc_count;
  1135. combined = (arelent **) xmalloc (total_count * sizeof (arelent *));
  1136. memcpy (combined, outsec->orelocation,
  1137. outsec->reloc_count * sizeof (arelent *));
  1138. memcpy (combined + outsec->reloc_count, relocs,
  1139. (size_t) (reloc_count * sizeof (arelent *)));
  1140. free (outsec->orelocation);
  1141. reloc_count = total_count;
  1142. relocs = combined;
  1143. }
  1144. bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
  1145. }
  1146. if (contents != NULL)
  1147. {
  1148. if (! bfd_set_section_contents (outbfd, outsec, contents,
  1149. insec->output_offset, size))
  1150. bfd_fatal (bfd_get_filename (outbfd));
  1151. free (contents);
  1152. }
  1153. /* Add this section to .nlmsections. */
  1154. if (! bfd_set_section_contents (outbfd, secsec, (void *) inname, secsecoff,
  1155. strlen (inname) + 1))
  1156. bfd_fatal (_("set .nlmsection contents"));
  1157. secsecoff += strlen (inname) + 1;
  1158. add = ((secsecoff + 3) &~ 3) - secsecoff;
  1159. if (add != 0)
  1160. {
  1161. bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
  1162. if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, add))
  1163. bfd_fatal (_("set .nlmsection contents"));
  1164. secsecoff += add;
  1165. }
  1166. if (contents != NULL)
  1167. bfd_h_put_32 (outbfd, (bfd_vma) outsec->filepos, buf);
  1168. else
  1169. bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
  1170. if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
  1171. bfd_fatal (_("set .nlmsection contents"));
  1172. secsecoff += 4;
  1173. bfd_h_put_32 (outbfd, (bfd_vma) size, buf);
  1174. if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
  1175. bfd_fatal (_("set .nlmsection contents"));
  1176. secsecoff += 4;
  1177. }
  1178. /* Some, perhaps all, NetWare targets require changing the relocs used
  1179. by the input formats. */
  1180. static void
  1181. mangle_relocs (bfd *outbfd, asection *insec, arelent ***relocs_ptr,
  1182. long *reloc_count_ptr, char *contents,
  1183. bfd_size_type contents_size)
  1184. {
  1185. switch (bfd_get_arch (outbfd))
  1186. {
  1187. #ifdef NLMCONV_I386
  1188. case bfd_arch_i386:
  1189. i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1190. contents, contents_size);
  1191. break;
  1192. #endif
  1193. #ifdef NLMCONV_ALPHA
  1194. case bfd_arch_alpha:
  1195. alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1196. contents, contents_size);
  1197. break;
  1198. #endif
  1199. #ifdef NLMCONV_POWERPC
  1200. case bfd_arch_powerpc:
  1201. powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1202. contents, contents_size);
  1203. break;
  1204. #endif
  1205. default:
  1206. default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1207. contents, contents_size);
  1208. break;
  1209. }
  1210. }
  1211. /* By default all we need to do for relocs is change the address by
  1212. the output_offset. */
  1213. static void
  1214. default_mangle_relocs (bfd *outbfd ATTRIBUTE_UNUSED, asection *insec,
  1215. arelent ***relocs_ptr, long *reloc_count_ptr,
  1216. char *contents ATTRIBUTE_UNUSED,
  1217. bfd_size_type contents_size ATTRIBUTE_UNUSED)
  1218. {
  1219. if (insec->output_offset != 0)
  1220. {
  1221. long reloc_count;
  1222. arelent **relocs;
  1223. long i;
  1224. reloc_count = *reloc_count_ptr;
  1225. relocs = *relocs_ptr;
  1226. for (i = 0; i < reloc_count; i++, relocs++)
  1227. (*relocs)->address += insec->output_offset;
  1228. }
  1229. }
  1230. #ifdef NLMCONV_I386
  1231. /* NetWare on the i386 supports a restricted set of relocs, which are
  1232. different from those used on other i386 targets. This routine
  1233. converts the relocs. It is, obviously, very target dependent. At
  1234. the moment, the nlm32-i386 backend performs similar translations;
  1235. however, it is more reliable and efficient to do them here. */
  1236. static reloc_howto_type nlm_i386_pcrel_howto =
  1237. HOWTO (1, /* type */
  1238. 0, /* rightshift */
  1239. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1240. 32, /* bitsize */
  1241. TRUE, /* pc_relative */
  1242. 0, /* bitpos */
  1243. complain_overflow_signed, /* complain_on_overflow */
  1244. 0, /* special_function */
  1245. "DISP32", /* name */
  1246. TRUE, /* partial_inplace */
  1247. 0xffffffff, /* src_mask */
  1248. 0xffffffff, /* dst_mask */
  1249. TRUE); /* pcrel_offset */
  1250. static void
  1251. i386_mangle_relocs (bfd *outbfd, asection *insec, arelent ***relocs_ptr,
  1252. long *reloc_count_ptr, char *contents,
  1253. bfd_size_type contents_size)
  1254. {
  1255. long reloc_count, i;
  1256. arelent **relocs;
  1257. reloc_count = *reloc_count_ptr;
  1258. relocs = *relocs_ptr;
  1259. for (i = 0; i < reloc_count; i++)
  1260. {
  1261. arelent *rel;
  1262. asymbol *sym;
  1263. bfd_size_type address;
  1264. bfd_vma addend;
  1265. rel = *relocs++;
  1266. /* PR 17512: file: 057f89c1. */
  1267. if (rel->sym_ptr_ptr == NULL)
  1268. continue;
  1269. sym = *rel->sym_ptr_ptr;
  1270. /* We're moving the relocs from the input section to the output
  1271. section, so we must adjust the address accordingly. */
  1272. address = rel->address;
  1273. rel->address += insec->output_offset;
  1274. /* Note that no serious harm will ensue if we fail to change a
  1275. reloc. The backend will fail when writing out the reloc. */
  1276. /* Make sure this reloc is within the data we have. We use only
  1277. 4 byte relocs here, so we insist on having 4 bytes. */
  1278. if (address + 4 > contents_size)
  1279. continue;
  1280. /* A PC relative reloc entirely within a single section is
  1281. completely unnecessary. This can be generated by ld -r. */
  1282. if (sym == insec->symbol
  1283. && rel->howto != NULL
  1284. && rel->howto->pc_relative
  1285. && ! rel->howto->pcrel_offset)
  1286. {
  1287. --*reloc_count_ptr;
  1288. --relocs;
  1289. memmove (relocs, relocs + 1,
  1290. (size_t) ((reloc_count - i) * sizeof (arelent *)));
  1291. continue;
  1292. }
  1293. /* Get the amount the relocation will add in. */
  1294. addend = rel->addend + sym->value;
  1295. /* NetWare doesn't support PC relative relocs against defined
  1296. symbols, so we have to eliminate them by doing the relocation
  1297. now. We can only do this if the reloc is within a single
  1298. section. */
  1299. if (rel->howto != NULL
  1300. && rel->howto->pc_relative
  1301. && bfd_get_section (sym) == insec->output_section)
  1302. {
  1303. bfd_vma val;
  1304. if (rel->howto->pcrel_offset)
  1305. addend -= address;
  1306. val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1307. val += addend;
  1308. bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1309. --*reloc_count_ptr;
  1310. --relocs;
  1311. memmove (relocs, relocs + 1,
  1312. (size_t) ((reloc_count - i) * sizeof (arelent *)));
  1313. continue;
  1314. }
  1315. /* NetWare doesn't support reloc addends, so we get rid of them
  1316. here by simply adding them into the object data. We handle
  1317. the symbol value, if any, the same way. */
  1318. if (addend != 0
  1319. && rel->howto != NULL
  1320. && rel->howto->rightshift == 0
  1321. && rel->howto->size == 2
  1322. && rel->howto->bitsize == 32
  1323. && rel->howto->bitpos == 0
  1324. && rel->howto->src_mask == 0xffffffff
  1325. && rel->howto->dst_mask == 0xffffffff)
  1326. {
  1327. bfd_vma val;
  1328. val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1329. val += addend;
  1330. bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1331. /* Adjust the reloc for the changes we just made. */
  1332. rel->addend = 0;
  1333. if (! bfd_is_und_section (bfd_get_section (sym)))
  1334. rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
  1335. }
  1336. /* NetWare uses a reloc with pcrel_offset set. We adjust
  1337. pc_relative relocs accordingly. We are going to change the
  1338. howto field, so we can only do this if the current one is
  1339. compatible. We should check that special_function is NULL
  1340. here, but at the moment coff-i386 uses a special_function
  1341. which does not affect what we are doing here. */
  1342. if (rel->howto != NULL
  1343. && rel->howto->pc_relative
  1344. && ! rel->howto->pcrel_offset
  1345. && rel->howto->rightshift == 0
  1346. && rel->howto->size == 2
  1347. && rel->howto->bitsize == 32
  1348. && rel->howto->bitpos == 0
  1349. && rel->howto->src_mask == 0xffffffff
  1350. && rel->howto->dst_mask == 0xffffffff)
  1351. {
  1352. bfd_vma val;
  1353. /* When pcrel_offset is not set, it means that the negative
  1354. of the address of the memory location is stored in the
  1355. memory location. We must add it back in. */
  1356. val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1357. val += address;
  1358. bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1359. /* We must change to a new howto. */
  1360. rel->howto = &nlm_i386_pcrel_howto;
  1361. }
  1362. }
  1363. }
  1364. #endif /* NLMCONV_I386 */
  1365. #ifdef NLMCONV_ALPHA
  1366. /* On the Alpha the first reloc for every section must be a special
  1367. relocs which hold the GP address. Also, the first reloc in the
  1368. file must be a special reloc which holds the address of the .lita
  1369. section. */
  1370. static reloc_howto_type nlm32_alpha_nw_howto =
  1371. HOWTO (ALPHA_R_NW_RELOC, /* type */
  1372. 0, /* rightshift */
  1373. 0, /* size (0 = byte, 1 = short, 2 = long) */
  1374. 0, /* bitsize */
  1375. FALSE, /* pc_relative */
  1376. 0, /* bitpos */
  1377. complain_overflow_dont, /* complain_on_overflow */
  1378. 0, /* special_function */
  1379. "NW_RELOC", /* name */
  1380. FALSE, /* partial_inplace */
  1381. 0, /* src_mask */
  1382. 0, /* dst_mask */
  1383. FALSE); /* pcrel_offset */
  1384. static void
  1385. alpha_mangle_relocs (bfd *outbfd, asection *insec,
  1386. arelent ***relocs_ptr, long *reloc_count_ptr,
  1387. char *contents ATTRIBUTE_UNUSED,
  1388. bfd_size_type contents_size ATTRIBUTE_UNUSED)
  1389. {
  1390. long old_reloc_count;
  1391. arelent **old_relocs;
  1392. arelent **relocs;
  1393. old_reloc_count = *reloc_count_ptr;
  1394. old_relocs = *relocs_ptr;
  1395. relocs = (arelent **) xmalloc ((old_reloc_count + 3) * sizeof (arelent *));
  1396. *relocs_ptr = relocs;
  1397. if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
  1398. {
  1399. bfd *inbfd;
  1400. asection *lita_section;
  1401. inbfd = insec->owner;
  1402. lita_section = bfd_get_section_by_name (inbfd, _LITA);
  1403. if (lita_section != (asection *) NULL)
  1404. {
  1405. nlm_alpha_backend_data (outbfd)->lita_address =
  1406. bfd_get_section_vma (inbfd, lita_section);
  1407. nlm_alpha_backend_data (outbfd)->lita_size =
  1408. bfd_section_size (inbfd, lita_section);
  1409. }
  1410. else
  1411. {
  1412. /* Avoid outputting this reloc again. */
  1413. nlm_alpha_backend_data (outbfd)->lita_address = 4;
  1414. }
  1415. *relocs = (arelent *) xmalloc (sizeof (arelent));
  1416. (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1417. (*relocs)->address = nlm_alpha_backend_data (outbfd)->lita_address;
  1418. (*relocs)->addend = nlm_alpha_backend_data (outbfd)->lita_size + 1;
  1419. (*relocs)->howto = &nlm32_alpha_nw_howto;
  1420. ++relocs;
  1421. ++(*reloc_count_ptr);
  1422. }
  1423. /* Get the GP value from bfd. */
  1424. if (nlm_alpha_backend_data (outbfd)->gp == 0)
  1425. nlm_alpha_backend_data (outbfd)->gp =
  1426. bfd_ecoff_get_gp_value (insec->owner);
  1427. *relocs = (arelent *) xmalloc (sizeof (arelent));
  1428. (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1429. (*relocs)->address = nlm_alpha_backend_data (outbfd)->gp;
  1430. (*relocs)->addend = 0;
  1431. (*relocs)->howto = &nlm32_alpha_nw_howto;
  1432. ++relocs;
  1433. ++(*reloc_count_ptr);
  1434. memcpy (relocs, old_relocs, (size_t) old_reloc_count * sizeof (arelent *));
  1435. relocs[old_reloc_count] = (arelent *) NULL;
  1436. free (old_relocs);
  1437. if (insec->output_offset != 0)
  1438. {
  1439. bfd_size_type i;
  1440. for (i = 0; i < (bfd_size_type) old_reloc_count; i++, relocs++)
  1441. (*relocs)->address += insec->output_offset;
  1442. }
  1443. }
  1444. #endif /* NLMCONV_ALPHA */
  1445. #ifdef NLMCONV_POWERPC
  1446. /* We keep a linked list of stubs which we must build. Because BFD
  1447. requires us to know the sizes of all sections before we can set the
  1448. contents of any, we must figure out which stubs we want to build
  1449. before we can actually build any of them. */
  1450. struct powerpc_stub
  1451. {
  1452. /* Next stub in linked list. */
  1453. struct powerpc_stub *next;
  1454. /* Symbol whose value is the start of the stub. This is a symbol
  1455. whose name begins with `.'. */
  1456. asymbol *start;
  1457. /* Symbol we are going to create a reloc against. This is a symbol
  1458. with the same name as START but without the leading `.'. */
  1459. asymbol *reloc;
  1460. /* The TOC index for this stub. This is the index into the TOC
  1461. section at which the reloc is created. */
  1462. unsigned int toc_index;
  1463. };
  1464. /* The linked list of stubs. */
  1465. static struct powerpc_stub *powerpc_stubs;
  1466. /* This is what a stub looks like. The first instruction will get
  1467. adjusted with the correct TOC index. */
  1468. static unsigned long powerpc_stub_insns[] =
  1469. {
  1470. 0x81820000, /* lwz r12,0(r2) */
  1471. 0x90410014, /* stw r2,20(r1) */
  1472. 0x800c0000, /* lwz r0,0(r12) */
  1473. 0x804c0004, /* lwz r2,r(r12) */
  1474. 0x7c0903a6, /* mtctr r0 */
  1475. 0x4e800420, /* bctr */
  1476. 0, /* Traceback table. */
  1477. 0xc8000,
  1478. 0
  1479. };
  1480. #define POWERPC_STUB_INSN_COUNT \
  1481. (sizeof powerpc_stub_insns / sizeof powerpc_stub_insns[0])
  1482. #define POWERPC_STUB_SIZE (4 * POWERPC_STUB_INSN_COUNT)
  1483. /* Each stub uses a four byte TOC entry. */
  1484. #define POWERPC_STUB_TOC_ENTRY_SIZE (4)
  1485. /* The original size of the .got section. */
  1486. static bfd_size_type powerpc_initial_got_size;
  1487. /* Look for all undefined symbols beginning with `.', and prepare to
  1488. build a stub for each one. */
  1489. static void
  1490. powerpc_build_stubs (bfd *inbfd, bfd *outbfd ATTRIBUTE_UNUSED,
  1491. asymbol ***symbols_ptr, long *symcount_ptr)
  1492. {
  1493. asection *stub_sec;
  1494. asection *got_sec;
  1495. unsigned int got_base;
  1496. long i;
  1497. long symcount;
  1498. long stubcount;
  1499. /* Make a section to hold stubs. We don't set SEC_HAS_CONTENTS for
  1500. the section to prevent copy_sections from reading from it. */
  1501. stub_sec = bfd_make_section_with_flags (inbfd, ".stubs",
  1502. (SEC_CODE
  1503. | SEC_RELOC
  1504. | SEC_ALLOC
  1505. | SEC_LOAD));
  1506. if (stub_sec == (asection *) NULL
  1507. || ! bfd_set_section_alignment (inbfd, stub_sec, 2))
  1508. bfd_fatal (".stubs");
  1509. /* Get the TOC section, which is named .got. */
  1510. got_sec = bfd_get_section_by_name (inbfd, ".got");
  1511. if (got_sec == (asection *) NULL)
  1512. {
  1513. got_sec = bfd_make_section_with_flags (inbfd, ".got",
  1514. (SEC_DATA
  1515. | SEC_RELOC
  1516. | SEC_ALLOC
  1517. | SEC_LOAD
  1518. | SEC_HAS_CONTENTS));
  1519. if (got_sec == (asection *) NULL
  1520. || ! bfd_set_section_alignment (inbfd, got_sec, 2))
  1521. bfd_fatal (".got");
  1522. }
  1523. powerpc_initial_got_size = bfd_section_size (inbfd, got_sec);
  1524. got_base = powerpc_initial_got_size;
  1525. got_base = (got_base + 3) &~ 3;
  1526. stubcount = 0;
  1527. symcount = *symcount_ptr;
  1528. for (i = 0; i < symcount; i++)
  1529. {
  1530. asymbol *sym;
  1531. asymbol *newsym;
  1532. char *newname;
  1533. struct powerpc_stub *item;
  1534. sym = (*symbols_ptr)[i];
  1535. /* We must make a stub for every undefined symbol whose name
  1536. starts with '.'. */
  1537. if (bfd_asymbol_name (sym)[0] != '.'
  1538. || ! bfd_is_und_section (bfd_get_section (sym)))
  1539. continue;
  1540. /* Make a new undefined symbol with the same name but without
  1541. the leading `.'. */
  1542. newsym = xmalloc (sizeof (asymbol));
  1543. *newsym = *sym;
  1544. newname = xmalloc (strlen (bfd_asymbol_name (sym)));
  1545. strcpy (newname, bfd_asymbol_name (sym) + 1);
  1546. newsym->name = newname;
  1547. /* Define the `.' symbol to be in the stub section. */
  1548. sym->section = stub_sec;
  1549. sym->value = stubcount * POWERPC_STUB_SIZE;
  1550. /* We set the BSF_DYNAMIC flag here so that we can check it when
  1551. we are mangling relocs. FIXME: This is a hack. */
  1552. sym->flags = BSF_LOCAL | BSF_DYNAMIC;
  1553. /* Add this stub to the linked list. */
  1554. item = (struct powerpc_stub *) xmalloc (sizeof (struct powerpc_stub));
  1555. item->start = sym;
  1556. item->reloc = newsym;
  1557. item->toc_index = got_base + stubcount * POWERPC_STUB_TOC_ENTRY_SIZE;
  1558. item->next = powerpc_stubs;
  1559. powerpc_stubs = item;
  1560. ++stubcount;
  1561. }
  1562. if (stubcount > 0)
  1563. {
  1564. asymbol **s;
  1565. struct powerpc_stub *l;
  1566. /* Add the new symbols we just created to the symbol table. */
  1567. *symbols_ptr = (asymbol **) xrealloc ((char *) *symbols_ptr,
  1568. ((symcount + stubcount)
  1569. * sizeof (asymbol)));
  1570. *symcount_ptr += stubcount;
  1571. s = &(*symbols_ptr)[symcount];
  1572. for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1573. *s++ = l->reloc;
  1574. /* Set the size of the .stubs section and increase the size of
  1575. the .got section. */
  1576. if (! bfd_set_section_size (inbfd, stub_sec,
  1577. stubcount * POWERPC_STUB_SIZE)
  1578. || ! bfd_set_section_size (inbfd, got_sec,
  1579. (got_base
  1580. + (stubcount
  1581. * POWERPC_STUB_TOC_ENTRY_SIZE))))
  1582. bfd_fatal (_("stub section sizes"));
  1583. }
  1584. }
  1585. /* Resolve all the stubs for PowerPC NetWare. We fill in the contents
  1586. of the output section, and create new relocs in the TOC. */
  1587. static void
  1588. powerpc_resolve_stubs (bfd *inbfd, bfd *outbfd)
  1589. {
  1590. bfd_byte buf[POWERPC_STUB_SIZE];
  1591. unsigned int i;
  1592. unsigned int stubcount;
  1593. arelent **relocs;
  1594. asection *got_sec;
  1595. arelent **r;
  1596. struct powerpc_stub *l;
  1597. if (powerpc_stubs == (struct powerpc_stub *) NULL)
  1598. return;
  1599. for (i = 0; i < POWERPC_STUB_INSN_COUNT; i++)
  1600. bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[i], buf + i * 4);
  1601. got_sec = bfd_get_section_by_name (inbfd, ".got");
  1602. assert (got_sec != (asection *) NULL);
  1603. assert (got_sec->output_section->orelocation == (arelent **) NULL);
  1604. stubcount = 0;
  1605. for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1606. ++stubcount;
  1607. relocs = (arelent **) xmalloc (stubcount * sizeof (arelent *));
  1608. r = relocs;
  1609. for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1610. {
  1611. arelent *reloc;
  1612. /* Adjust the first instruction to use the right TOC index. */
  1613. bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[0] + l->toc_index, buf);
  1614. /* Write this stub out. */
  1615. if (! bfd_set_section_contents (outbfd,
  1616. bfd_get_section (l->start),
  1617. buf,
  1618. l->start->value,
  1619. POWERPC_STUB_SIZE))
  1620. bfd_fatal (_("writing stub"));
  1621. /* Create a new reloc for the TOC entry. */
  1622. reloc = (arelent *) xmalloc (sizeof (arelent));
  1623. reloc->sym_ptr_ptr = &l->reloc;
  1624. reloc->address = l->toc_index + got_sec->output_offset;
  1625. reloc->addend = 0;
  1626. reloc->howto = bfd_reloc_type_lookup (inbfd, BFD_RELOC_32);
  1627. *r++ = reloc;
  1628. }
  1629. bfd_set_reloc (outbfd, got_sec->output_section, relocs, stubcount);
  1630. }
  1631. /* Adjust relocation entries for PowerPC NetWare. We do not output
  1632. TOC relocations. The object code already contains the offset from
  1633. the TOC pointer. When the function is called, the TOC register,
  1634. r2, will be set to the correct TOC value, so there is no need for
  1635. any further reloc. */
  1636. static void
  1637. powerpc_mangle_relocs (bfd *outbfd, asection *insec,
  1638. arelent ***relocs_ptr,
  1639. long *reloc_count_ptr, char *contents,
  1640. bfd_size_type contents_size ATTRIBUTE_UNUSED)
  1641. {
  1642. reloc_howto_type *toc_howto;
  1643. long reloc_count;
  1644. arelent **relocs;
  1645. long i;
  1646. toc_howto = bfd_reloc_type_lookup (insec->owner, BFD_RELOC_PPC_TOC16);
  1647. if (toc_howto == (reloc_howto_type *) NULL)
  1648. fatal (_("Unable to locate PPC_TOC16 reloc information"));
  1649. /* If this is the .got section, clear out all the contents beyond
  1650. the initial size. We must do this here because copy_sections is
  1651. going to write out whatever we return in the contents field. */
  1652. if (strcmp (bfd_get_section_name (insec->owner, insec), ".got") == 0)
  1653. memset (contents + powerpc_initial_got_size, 0,
  1654. (size_t) (bfd_get_section_size (insec) - powerpc_initial_got_size));
  1655. reloc_count = *reloc_count_ptr;
  1656. relocs = *relocs_ptr;
  1657. for (i = 0; i < reloc_count; i++)
  1658. {
  1659. arelent *rel;
  1660. asymbol *sym;
  1661. bfd_vma sym_value;
  1662. rel = *relocs++;
  1663. sym = *rel->sym_ptr_ptr;
  1664. /* Convert any relocs against the .bss section into relocs
  1665. against the .data section. */
  1666. if (strcmp (bfd_get_section_name (outbfd, bfd_get_section (sym)),
  1667. NLM_UNINITIALIZED_DATA_NAME) == 0)
  1668. {
  1669. asection *datasec;
  1670. datasec = bfd_get_section_by_name (outbfd,
  1671. NLM_INITIALIZED_DATA_NAME);
  1672. if (datasec != NULL)
  1673. {
  1674. rel->addend += (bfd_get_section_vma (outbfd,
  1675. bfd_get_section (sym))
  1676. + sym->value);
  1677. rel->sym_ptr_ptr = datasec->symbol_ptr_ptr;
  1678. sym = *rel->sym_ptr_ptr;
  1679. }
  1680. }
  1681. /* PR 17512: file: 70cfde95. */
  1682. if (rel->howto == NULL)
  1683. continue;
  1684. /* We must be able to resolve all PC relative relocs at this
  1685. point. If we get a branch to an undefined symbol we build a
  1686. stub, since NetWare will resolve undefined symbols into a
  1687. pointer to a function descriptor. */
  1688. if (rel->howto->pc_relative)
  1689. {
  1690. /* This check for whether a symbol is in the same section as
  1691. the reloc will be wrong if there is a PC relative reloc
  1692. between two sections both of which were placed in the
  1693. same output section. This should not happen. */
  1694. if (bfd_get_section (sym) != insec->output_section)
  1695. non_fatal (_("unresolved PC relative reloc against %s"),
  1696. bfd_asymbol_name (sym));
  1697. else
  1698. {
  1699. bfd_vma val;
  1700. if (rel->address > contents_size - 4)
  1701. {
  1702. non_fatal (_("Out of range relocation: %lx"),
  1703. (long) rel->address);
  1704. break;
  1705. }
  1706. assert (rel->howto->size == 2 && rel->howto->pcrel_offset);
  1707. val = bfd_get_32 (outbfd, (bfd_byte *) contents + rel->address);
  1708. val = ((val &~ rel->howto->dst_mask)
  1709. | (((val & rel->howto->src_mask)
  1710. + (sym->value - rel->address)
  1711. + rel->addend)
  1712. & rel->howto->dst_mask));
  1713. bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
  1714. /* If this reloc is against an stubbed symbol and the
  1715. next instruction is
  1716. cror 31,31,31
  1717. then we replace the next instruction with
  1718. lwz r2,20(r1)
  1719. This reloads the TOC pointer after a stub call. */
  1720. if (bfd_asymbol_name (sym)[0] == '.'
  1721. && (sym->flags & BSF_DYNAMIC) != 0
  1722. && (bfd_get_32 (outbfd,
  1723. (bfd_byte *) contents + rel->address + 4)
  1724. == 0x4ffffb82)) /* cror 31,31,31 */
  1725. bfd_put_32 (outbfd, (bfd_vma) 0x80410014, /* lwz r2,20(r1) */
  1726. (bfd_byte *) contents + rel->address + 4);
  1727. --*reloc_count_ptr;
  1728. --relocs;
  1729. memmove (relocs, relocs + 1,
  1730. (size_t) ((reloc_count - 1) * sizeof (arelent *)));
  1731. continue;
  1732. }
  1733. }
  1734. /* When considering a TOC reloc, we do not want to include the
  1735. symbol value. The symbol will be start of the TOC section
  1736. (which is named .got). We do want to include the addend. */
  1737. if (rel->howto == toc_howto)
  1738. sym_value = 0;
  1739. else
  1740. sym_value = sym->value;
  1741. /* If this is a relocation against a symbol with a value, or
  1742. there is a reloc addend, we need to update the addend in the
  1743. object file. */
  1744. if (sym_value + rel->addend != 0)
  1745. {
  1746. bfd_vma val;
  1747. switch (rel->howto->size)
  1748. {
  1749. case 1:
  1750. if (rel->address > contents_size - 2)
  1751. {
  1752. non_fatal (_("Out of range relocation: %lx"),
  1753. (long) rel->address);
  1754. break;
  1755. }
  1756. val = bfd_get_16 (outbfd,
  1757. (bfd_byte *) contents + rel->address);
  1758. val = ((val &~ rel->howto->dst_mask)
  1759. | (((val & rel->howto->src_mask)
  1760. + sym_value
  1761. + rel->addend)
  1762. & rel->howto->dst_mask));
  1763. if ((bfd_signed_vma) val < - 0x8000
  1764. || (bfd_signed_vma) val >= 0x8000)
  1765. non_fatal (_("overflow when adjusting relocation against %s"),
  1766. bfd_asymbol_name (sym));
  1767. bfd_put_16 (outbfd, val, (bfd_byte *) contents + rel->address);
  1768. break;
  1769. case 2:
  1770. /* PR 17512: file: 0455a112. */
  1771. if (rel->address > contents_size - 4)
  1772. {
  1773. non_fatal (_("Out of range relocation: %lx"),
  1774. (long) rel->address);
  1775. break;
  1776. }
  1777. val = bfd_get_32 (outbfd,
  1778. (bfd_byte *) contents + rel->address);
  1779. val = ((val &~ rel->howto->dst_mask)
  1780. | (((val & rel->howto->src_mask)
  1781. + sym_value
  1782. + rel->addend)
  1783. & rel->howto->dst_mask));
  1784. bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
  1785. break;
  1786. default:
  1787. fatal (_("Unsupported relocation size: %d"), rel->howto->size);
  1788. }
  1789. if (! bfd_is_und_section (bfd_get_section (sym)))
  1790. rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
  1791. rel->addend = 0;
  1792. }
  1793. /* Now that we have incorporated the addend, remove any TOC
  1794. relocs. */
  1795. if (rel->howto == toc_howto)
  1796. {
  1797. --*reloc_count_ptr;
  1798. --relocs;
  1799. memmove (relocs, relocs + 1,
  1800. (size_t) ((reloc_count - i) * sizeof (arelent *)));
  1801. continue;
  1802. }
  1803. rel->address += insec->output_offset;
  1804. }
  1805. }
  1806. #endif /* NLMCONV_POWERPC */
  1807. /* Name of linker. */
  1808. #ifndef LD_NAME
  1809. #define LD_NAME "ld"
  1810. #endif
  1811. /* The user has specified several input files. Invoke the linker to
  1812. link them all together, and convert and delete the resulting output
  1813. file. */
  1814. static char *
  1815. link_inputs (struct string_list *inputs, char *ld, char * mfile)
  1816. {
  1817. size_t c;
  1818. struct string_list *q;
  1819. char **argv;
  1820. size_t i;
  1821. int pid;
  1822. int status;
  1823. char *errfmt;
  1824. char *errarg;
  1825. c = 0;
  1826. for (q = inputs; q != NULL; q = q->next)
  1827. ++c;
  1828. argv = (char **) alloca ((c + 7) * sizeof (char *));
  1829. #ifndef __MSDOS__
  1830. if (ld == NULL)
  1831. {
  1832. char *p;
  1833. /* Find the linker to invoke based on how nlmconv was run. */
  1834. p = program_name + strlen (program_name);
  1835. while (p != program_name)
  1836. {
  1837. if (p[-1] == '/')
  1838. {
  1839. ld = (char *) xmalloc (p - program_name + strlen (LD_NAME) + 1);
  1840. memcpy (ld, program_name, p - program_name);
  1841. strcpy (ld + (p - program_name), LD_NAME);
  1842. break;
  1843. }
  1844. --p;
  1845. }
  1846. }
  1847. #endif
  1848. if (ld == NULL)
  1849. ld = (char *) LD_NAME;
  1850. unlink_on_exit = make_temp_file (".O");
  1851. argv[0] = ld;
  1852. argv[1] = (char *) "-Ur";
  1853. argv[2] = (char *) "-o";
  1854. argv[3] = unlink_on_exit;
  1855. /* If we have been given the name of a mapfile and that
  1856. name is not 'stderr' then pass it on to the linker. */
  1857. if (mfile
  1858. && * mfile
  1859. && strcmp (mfile, "stderr") == 0)
  1860. {
  1861. argv[4] = (char *) "-Map";
  1862. argv[5] = mfile;
  1863. i = 6;
  1864. }
  1865. else
  1866. i = 4;
  1867. for (q = inputs; q != NULL; q = q->next, i++)
  1868. argv[i] = q->string;
  1869. argv[i] = NULL;
  1870. if (debug)
  1871. {
  1872. for (i = 0; argv[i] != NULL; i++)
  1873. fprintf (stderr, " %s", argv[i]);
  1874. fprintf (stderr, "\n");
  1875. }
  1876. pid = pexecute (ld, argv, program_name, (char *) NULL, &errfmt, &errarg,
  1877. PEXECUTE_SEARCH | PEXECUTE_ONE);
  1878. if (pid == -1)
  1879. {
  1880. fprintf (stderr, _("%s: execution of %s failed: "), program_name, ld);
  1881. fprintf (stderr, errfmt, errarg);
  1882. unlink (unlink_on_exit);
  1883. exit (1);
  1884. }
  1885. if (pwait (pid, &status, 0) < 0)
  1886. {
  1887. perror ("pwait");
  1888. unlink (unlink_on_exit);
  1889. exit (1);
  1890. }
  1891. if (status != 0)
  1892. {
  1893. non_fatal (_("Execution of %s failed"), ld);
  1894. unlink (unlink_on_exit);
  1895. exit (1);
  1896. }
  1897. return unlink_on_exit;
  1898. }