nlmconv.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. /* nlmconv.c -- NLM conversion program
  2. Copyright (C) 1993-2017 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. bfd_byte *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. if (!bfd_malloc_and_get_section (inbfd, insec, &contents))
  1112. bfd_fatal (bfd_get_filename (inbfd));
  1113. }
  1114. reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
  1115. if (reloc_size < 0)
  1116. bfd_fatal (bfd_get_filename (inbfd));
  1117. if (reloc_size != 0)
  1118. {
  1119. arelent **relocs;
  1120. long reloc_count;
  1121. relocs = (arelent **) xmalloc (reloc_size);
  1122. reloc_count = bfd_canonicalize_reloc (inbfd, insec, relocs, symbols);
  1123. if (reloc_count < 0)
  1124. bfd_fatal (bfd_get_filename (inbfd));
  1125. mangle_relocs (outbfd, insec, &relocs, &reloc_count, (char *) contents,
  1126. size);
  1127. /* FIXME: refers to internal BFD fields. */
  1128. if (outsec->orelocation != (arelent **) NULL)
  1129. {
  1130. bfd_size_type total_count;
  1131. arelent **combined;
  1132. total_count = reloc_count + outsec->reloc_count;
  1133. combined = (arelent **) xmalloc (total_count * sizeof (arelent *));
  1134. memcpy (combined, outsec->orelocation,
  1135. outsec->reloc_count * sizeof (arelent *));
  1136. memcpy (combined + outsec->reloc_count, relocs,
  1137. (size_t) (reloc_count * sizeof (arelent *)));
  1138. free (outsec->orelocation);
  1139. reloc_count = total_count;
  1140. relocs = combined;
  1141. }
  1142. bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
  1143. }
  1144. if (contents != NULL)
  1145. {
  1146. if (! bfd_set_section_contents (outbfd, outsec, contents,
  1147. insec->output_offset, size))
  1148. bfd_fatal (bfd_get_filename (outbfd));
  1149. free (contents);
  1150. }
  1151. /* Add this section to .nlmsections. */
  1152. if (! bfd_set_section_contents (outbfd, secsec, (void *) inname, secsecoff,
  1153. strlen (inname) + 1))
  1154. bfd_fatal (_("set .nlmsection contents"));
  1155. secsecoff += strlen (inname) + 1;
  1156. add = ((secsecoff + 3) &~ 3) - secsecoff;
  1157. if (add != 0)
  1158. {
  1159. bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
  1160. if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, add))
  1161. bfd_fatal (_("set .nlmsection contents"));
  1162. secsecoff += add;
  1163. }
  1164. if (contents != NULL)
  1165. bfd_h_put_32 (outbfd, (bfd_vma) outsec->filepos, buf);
  1166. else
  1167. bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
  1168. if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
  1169. bfd_fatal (_("set .nlmsection contents"));
  1170. secsecoff += 4;
  1171. bfd_h_put_32 (outbfd, (bfd_vma) size, buf);
  1172. if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
  1173. bfd_fatal (_("set .nlmsection contents"));
  1174. secsecoff += 4;
  1175. }
  1176. /* Some, perhaps all, NetWare targets require changing the relocs used
  1177. by the input formats. */
  1178. static void
  1179. mangle_relocs (bfd *outbfd, asection *insec, arelent ***relocs_ptr,
  1180. long *reloc_count_ptr, char *contents,
  1181. bfd_size_type contents_size)
  1182. {
  1183. switch (bfd_get_arch (outbfd))
  1184. {
  1185. #ifdef NLMCONV_I386
  1186. case bfd_arch_i386:
  1187. i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1188. contents, contents_size);
  1189. break;
  1190. #endif
  1191. #ifdef NLMCONV_ALPHA
  1192. case bfd_arch_alpha:
  1193. alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1194. contents, contents_size);
  1195. break;
  1196. #endif
  1197. #ifdef NLMCONV_POWERPC
  1198. case bfd_arch_powerpc:
  1199. powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1200. contents, contents_size);
  1201. break;
  1202. #endif
  1203. default:
  1204. default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1205. contents, contents_size);
  1206. break;
  1207. }
  1208. }
  1209. /* By default all we need to do for relocs is change the address by
  1210. the output_offset. */
  1211. static void
  1212. default_mangle_relocs (bfd *outbfd ATTRIBUTE_UNUSED, asection *insec,
  1213. arelent ***relocs_ptr, long *reloc_count_ptr,
  1214. char *contents ATTRIBUTE_UNUSED,
  1215. bfd_size_type contents_size ATTRIBUTE_UNUSED)
  1216. {
  1217. if (insec->output_offset != 0)
  1218. {
  1219. long reloc_count;
  1220. arelent **relocs;
  1221. long i;
  1222. reloc_count = *reloc_count_ptr;
  1223. relocs = *relocs_ptr;
  1224. for (i = 0; i < reloc_count; i++, relocs++)
  1225. (*relocs)->address += insec->output_offset;
  1226. }
  1227. }
  1228. #ifdef NLMCONV_I386
  1229. /* NetWare on the i386 supports a restricted set of relocs, which are
  1230. different from those used on other i386 targets. This routine
  1231. converts the relocs. It is, obviously, very target dependent. At
  1232. the moment, the nlm32-i386 backend performs similar translations;
  1233. however, it is more reliable and efficient to do them here. */
  1234. static reloc_howto_type nlm_i386_pcrel_howto =
  1235. HOWTO (1, /* type */
  1236. 0, /* rightshift */
  1237. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1238. 32, /* bitsize */
  1239. TRUE, /* pc_relative */
  1240. 0, /* bitpos */
  1241. complain_overflow_signed, /* complain_on_overflow */
  1242. 0, /* special_function */
  1243. "DISP32", /* name */
  1244. TRUE, /* partial_inplace */
  1245. 0xffffffff, /* src_mask */
  1246. 0xffffffff, /* dst_mask */
  1247. TRUE); /* pcrel_offset */
  1248. static void
  1249. i386_mangle_relocs (bfd *outbfd, asection *insec, arelent ***relocs_ptr,
  1250. long *reloc_count_ptr, char *contents,
  1251. bfd_size_type contents_size)
  1252. {
  1253. long reloc_count, i;
  1254. arelent **relocs;
  1255. reloc_count = *reloc_count_ptr;
  1256. relocs = *relocs_ptr;
  1257. for (i = 0; i < reloc_count; i++)
  1258. {
  1259. arelent *rel;
  1260. asymbol *sym;
  1261. bfd_size_type address;
  1262. bfd_vma addend;
  1263. rel = *relocs++;
  1264. /* PR 17512: file: 057f89c1. */
  1265. if (rel->sym_ptr_ptr == NULL)
  1266. continue;
  1267. sym = *rel->sym_ptr_ptr;
  1268. /* We're moving the relocs from the input section to the output
  1269. section, so we must adjust the address accordingly. */
  1270. address = rel->address;
  1271. rel->address += insec->output_offset;
  1272. /* Note that no serious harm will ensue if we fail to change a
  1273. reloc. The backend will fail when writing out the reloc. */
  1274. /* Make sure this reloc is within the data we have. We use only
  1275. 4 byte relocs here, so we insist on having 4 bytes. */
  1276. if (address + 4 > contents_size)
  1277. continue;
  1278. /* A PC relative reloc entirely within a single section is
  1279. completely unnecessary. This can be generated by ld -r. */
  1280. if (sym == insec->symbol
  1281. && rel->howto != NULL
  1282. && rel->howto->pc_relative
  1283. && ! rel->howto->pcrel_offset)
  1284. {
  1285. --*reloc_count_ptr;
  1286. --relocs;
  1287. memmove (relocs, relocs + 1,
  1288. (size_t) ((reloc_count - i) * sizeof (arelent *)));
  1289. continue;
  1290. }
  1291. /* Get the amount the relocation will add in. */
  1292. addend = rel->addend + sym->value;
  1293. /* NetWare doesn't support PC relative relocs against defined
  1294. symbols, so we have to eliminate them by doing the relocation
  1295. now. We can only do this if the reloc is within a single
  1296. section. */
  1297. if (rel->howto != NULL
  1298. && rel->howto->pc_relative
  1299. && bfd_get_section (sym) == insec->output_section)
  1300. {
  1301. bfd_vma val;
  1302. if (rel->howto->pcrel_offset)
  1303. addend -= address;
  1304. val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1305. val += addend;
  1306. bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1307. --*reloc_count_ptr;
  1308. --relocs;
  1309. memmove (relocs, relocs + 1,
  1310. (size_t) ((reloc_count - i) * sizeof (arelent *)));
  1311. continue;
  1312. }
  1313. /* NetWare doesn't support reloc addends, so we get rid of them
  1314. here by simply adding them into the object data. We handle
  1315. the symbol value, if any, the same way. */
  1316. if (addend != 0
  1317. && rel->howto != NULL
  1318. && rel->howto->rightshift == 0
  1319. && rel->howto->size == 2
  1320. && rel->howto->bitsize == 32
  1321. && rel->howto->bitpos == 0
  1322. && rel->howto->src_mask == 0xffffffff
  1323. && rel->howto->dst_mask == 0xffffffff)
  1324. {
  1325. bfd_vma val;
  1326. val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1327. val += addend;
  1328. bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1329. /* Adjust the reloc for the changes we just made. */
  1330. rel->addend = 0;
  1331. if (! bfd_is_und_section (bfd_get_section (sym)))
  1332. rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
  1333. }
  1334. /* NetWare uses a reloc with pcrel_offset set. We adjust
  1335. pc_relative relocs accordingly. We are going to change the
  1336. howto field, so we can only do this if the current one is
  1337. compatible. We should check that special_function is NULL
  1338. here, but at the moment coff-i386 uses a special_function
  1339. which does not affect what we are doing here. */
  1340. if (rel->howto != NULL
  1341. && rel->howto->pc_relative
  1342. && ! rel->howto->pcrel_offset
  1343. && rel->howto->rightshift == 0
  1344. && rel->howto->size == 2
  1345. && rel->howto->bitsize == 32
  1346. && rel->howto->bitpos == 0
  1347. && rel->howto->src_mask == 0xffffffff
  1348. && rel->howto->dst_mask == 0xffffffff)
  1349. {
  1350. bfd_vma val;
  1351. /* When pcrel_offset is not set, it means that the negative
  1352. of the address of the memory location is stored in the
  1353. memory location. We must add it back in. */
  1354. val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1355. val += address;
  1356. bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1357. /* We must change to a new howto. */
  1358. rel->howto = &nlm_i386_pcrel_howto;
  1359. }
  1360. }
  1361. }
  1362. #endif /* NLMCONV_I386 */
  1363. #ifdef NLMCONV_ALPHA
  1364. /* On the Alpha the first reloc for every section must be a special
  1365. relocs which hold the GP address. Also, the first reloc in the
  1366. file must be a special reloc which holds the address of the .lita
  1367. section. */
  1368. static reloc_howto_type nlm32_alpha_nw_howto =
  1369. HOWTO (ALPHA_R_NW_RELOC, /* type */
  1370. 0, /* rightshift */
  1371. 0, /* size (0 = byte, 1 = short, 2 = long) */
  1372. 0, /* bitsize */
  1373. FALSE, /* pc_relative */
  1374. 0, /* bitpos */
  1375. complain_overflow_dont, /* complain_on_overflow */
  1376. 0, /* special_function */
  1377. "NW_RELOC", /* name */
  1378. FALSE, /* partial_inplace */
  1379. 0, /* src_mask */
  1380. 0, /* dst_mask */
  1381. FALSE); /* pcrel_offset */
  1382. static void
  1383. alpha_mangle_relocs (bfd *outbfd, asection *insec,
  1384. arelent ***relocs_ptr, long *reloc_count_ptr,
  1385. char *contents ATTRIBUTE_UNUSED,
  1386. bfd_size_type contents_size ATTRIBUTE_UNUSED)
  1387. {
  1388. long old_reloc_count;
  1389. arelent **old_relocs;
  1390. arelent **relocs;
  1391. old_reloc_count = *reloc_count_ptr;
  1392. old_relocs = *relocs_ptr;
  1393. relocs = (arelent **) xmalloc ((old_reloc_count + 3) * sizeof (arelent *));
  1394. *relocs_ptr = relocs;
  1395. if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
  1396. {
  1397. bfd *inbfd;
  1398. asection *lita_section;
  1399. inbfd = insec->owner;
  1400. lita_section = bfd_get_section_by_name (inbfd, _LITA);
  1401. if (lita_section != (asection *) NULL)
  1402. {
  1403. nlm_alpha_backend_data (outbfd)->lita_address =
  1404. bfd_get_section_vma (inbfd, lita_section);
  1405. nlm_alpha_backend_data (outbfd)->lita_size =
  1406. bfd_section_size (inbfd, lita_section);
  1407. }
  1408. else
  1409. {
  1410. /* Avoid outputting this reloc again. */
  1411. nlm_alpha_backend_data (outbfd)->lita_address = 4;
  1412. }
  1413. *relocs = (arelent *) xmalloc (sizeof (arelent));
  1414. (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1415. (*relocs)->address = nlm_alpha_backend_data (outbfd)->lita_address;
  1416. (*relocs)->addend = nlm_alpha_backend_data (outbfd)->lita_size + 1;
  1417. (*relocs)->howto = &nlm32_alpha_nw_howto;
  1418. ++relocs;
  1419. ++(*reloc_count_ptr);
  1420. }
  1421. /* Get the GP value from bfd. */
  1422. if (nlm_alpha_backend_data (outbfd)->gp == 0)
  1423. nlm_alpha_backend_data (outbfd)->gp =
  1424. bfd_ecoff_get_gp_value (insec->owner);
  1425. *relocs = (arelent *) xmalloc (sizeof (arelent));
  1426. (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1427. (*relocs)->address = nlm_alpha_backend_data (outbfd)->gp;
  1428. (*relocs)->addend = 0;
  1429. (*relocs)->howto = &nlm32_alpha_nw_howto;
  1430. ++relocs;
  1431. ++(*reloc_count_ptr);
  1432. memcpy (relocs, old_relocs, (size_t) old_reloc_count * sizeof (arelent *));
  1433. relocs[old_reloc_count] = (arelent *) NULL;
  1434. free (old_relocs);
  1435. if (insec->output_offset != 0)
  1436. {
  1437. bfd_size_type i;
  1438. for (i = 0; i < (bfd_size_type) old_reloc_count; i++, relocs++)
  1439. (*relocs)->address += insec->output_offset;
  1440. }
  1441. }
  1442. #endif /* NLMCONV_ALPHA */
  1443. #ifdef NLMCONV_POWERPC
  1444. /* We keep a linked list of stubs which we must build. Because BFD
  1445. requires us to know the sizes of all sections before we can set the
  1446. contents of any, we must figure out which stubs we want to build
  1447. before we can actually build any of them. */
  1448. struct powerpc_stub
  1449. {
  1450. /* Next stub in linked list. */
  1451. struct powerpc_stub *next;
  1452. /* Symbol whose value is the start of the stub. This is a symbol
  1453. whose name begins with `.'. */
  1454. asymbol *start;
  1455. /* Symbol we are going to create a reloc against. This is a symbol
  1456. with the same name as START but without the leading `.'. */
  1457. asymbol *reloc;
  1458. /* The TOC index for this stub. This is the index into the TOC
  1459. section at which the reloc is created. */
  1460. unsigned int toc_index;
  1461. };
  1462. /* The linked list of stubs. */
  1463. static struct powerpc_stub *powerpc_stubs;
  1464. /* This is what a stub looks like. The first instruction will get
  1465. adjusted with the correct TOC index. */
  1466. static unsigned long powerpc_stub_insns[] =
  1467. {
  1468. 0x81820000, /* lwz r12,0(r2) */
  1469. 0x90410014, /* stw r2,20(r1) */
  1470. 0x800c0000, /* lwz r0,0(r12) */
  1471. 0x804c0004, /* lwz r2,r(r12) */
  1472. 0x7c0903a6, /* mtctr r0 */
  1473. 0x4e800420, /* bctr */
  1474. 0, /* Traceback table. */
  1475. 0xc8000,
  1476. 0
  1477. };
  1478. #define POWERPC_STUB_INSN_COUNT \
  1479. (sizeof powerpc_stub_insns / sizeof powerpc_stub_insns[0])
  1480. #define POWERPC_STUB_SIZE (4 * POWERPC_STUB_INSN_COUNT)
  1481. /* Each stub uses a four byte TOC entry. */
  1482. #define POWERPC_STUB_TOC_ENTRY_SIZE (4)
  1483. /* The original size of the .got section. */
  1484. static bfd_size_type powerpc_initial_got_size;
  1485. /* Look for all undefined symbols beginning with `.', and prepare to
  1486. build a stub for each one. */
  1487. static void
  1488. powerpc_build_stubs (bfd *inbfd, bfd *outbfd ATTRIBUTE_UNUSED,
  1489. asymbol ***symbols_ptr, long *symcount_ptr)
  1490. {
  1491. asection *stub_sec;
  1492. asection *got_sec;
  1493. unsigned int got_base;
  1494. long i;
  1495. long symcount;
  1496. long stubcount;
  1497. /* Make a section to hold stubs. We don't set SEC_HAS_CONTENTS for
  1498. the section to prevent copy_sections from reading from it. */
  1499. stub_sec = bfd_make_section_with_flags (inbfd, ".stubs",
  1500. (SEC_CODE
  1501. | SEC_RELOC
  1502. | SEC_ALLOC
  1503. | SEC_LOAD));
  1504. if (stub_sec == (asection *) NULL
  1505. || ! bfd_set_section_alignment (inbfd, stub_sec, 2))
  1506. bfd_fatal (".stubs");
  1507. /* Get the TOC section, which is named .got. */
  1508. got_sec = bfd_get_section_by_name (inbfd, ".got");
  1509. if (got_sec == (asection *) NULL)
  1510. {
  1511. got_sec = bfd_make_section_with_flags (inbfd, ".got",
  1512. (SEC_DATA
  1513. | SEC_RELOC
  1514. | SEC_ALLOC
  1515. | SEC_LOAD
  1516. | SEC_HAS_CONTENTS));
  1517. if (got_sec == (asection *) NULL
  1518. || ! bfd_set_section_alignment (inbfd, got_sec, 2))
  1519. bfd_fatal (".got");
  1520. }
  1521. powerpc_initial_got_size = bfd_section_size (inbfd, got_sec);
  1522. got_base = powerpc_initial_got_size;
  1523. got_base = (got_base + 3) &~ 3;
  1524. stubcount = 0;
  1525. symcount = *symcount_ptr;
  1526. for (i = 0; i < symcount; i++)
  1527. {
  1528. asymbol *sym;
  1529. asymbol *newsym;
  1530. char *newname;
  1531. struct powerpc_stub *item;
  1532. sym = (*symbols_ptr)[i];
  1533. /* We must make a stub for every undefined symbol whose name
  1534. starts with '.'. */
  1535. if (bfd_asymbol_name (sym)[0] != '.'
  1536. || ! bfd_is_und_section (bfd_get_section (sym)))
  1537. continue;
  1538. /* Make a new undefined symbol with the same name but without
  1539. the leading `.'. */
  1540. newsym = xmalloc (sizeof (asymbol));
  1541. *newsym = *sym;
  1542. newname = xmalloc (strlen (bfd_asymbol_name (sym)));
  1543. strcpy (newname, bfd_asymbol_name (sym) + 1);
  1544. newsym->name = newname;
  1545. /* Define the `.' symbol to be in the stub section. */
  1546. sym->section = stub_sec;
  1547. sym->value = stubcount * POWERPC_STUB_SIZE;
  1548. /* We set the BSF_DYNAMIC flag here so that we can check it when
  1549. we are mangling relocs. FIXME: This is a hack. */
  1550. sym->flags = BSF_LOCAL | BSF_DYNAMIC;
  1551. /* Add this stub to the linked list. */
  1552. item = (struct powerpc_stub *) xmalloc (sizeof (struct powerpc_stub));
  1553. item->start = sym;
  1554. item->reloc = newsym;
  1555. item->toc_index = got_base + stubcount * POWERPC_STUB_TOC_ENTRY_SIZE;
  1556. item->next = powerpc_stubs;
  1557. powerpc_stubs = item;
  1558. ++stubcount;
  1559. }
  1560. if (stubcount > 0)
  1561. {
  1562. asymbol **s;
  1563. struct powerpc_stub *l;
  1564. /* Add the new symbols we just created to the symbol table. */
  1565. *symbols_ptr = (asymbol **) xrealloc ((char *) *symbols_ptr,
  1566. ((symcount + stubcount)
  1567. * sizeof (asymbol)));
  1568. *symcount_ptr += stubcount;
  1569. s = &(*symbols_ptr)[symcount];
  1570. for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1571. *s++ = l->reloc;
  1572. /* Set the size of the .stubs section and increase the size of
  1573. the .got section. */
  1574. if (! bfd_set_section_size (inbfd, stub_sec,
  1575. stubcount * POWERPC_STUB_SIZE)
  1576. || ! bfd_set_section_size (inbfd, got_sec,
  1577. (got_base
  1578. + (stubcount
  1579. * POWERPC_STUB_TOC_ENTRY_SIZE))))
  1580. bfd_fatal (_("stub section sizes"));
  1581. }
  1582. }
  1583. /* Resolve all the stubs for PowerPC NetWare. We fill in the contents
  1584. of the output section, and create new relocs in the TOC. */
  1585. static void
  1586. powerpc_resolve_stubs (bfd *inbfd, bfd *outbfd)
  1587. {
  1588. bfd_byte buf[POWERPC_STUB_SIZE];
  1589. unsigned int i;
  1590. unsigned int stubcount;
  1591. arelent **relocs;
  1592. asection *got_sec;
  1593. arelent **r;
  1594. struct powerpc_stub *l;
  1595. if (powerpc_stubs == (struct powerpc_stub *) NULL)
  1596. return;
  1597. for (i = 0; i < POWERPC_STUB_INSN_COUNT; i++)
  1598. bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[i], buf + i * 4);
  1599. got_sec = bfd_get_section_by_name (inbfd, ".got");
  1600. assert (got_sec != (asection *) NULL);
  1601. assert (got_sec->output_section->orelocation == (arelent **) NULL);
  1602. stubcount = 0;
  1603. for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1604. ++stubcount;
  1605. relocs = (arelent **) xmalloc (stubcount * sizeof (arelent *));
  1606. r = relocs;
  1607. for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1608. {
  1609. arelent *reloc;
  1610. /* Adjust the first instruction to use the right TOC index. */
  1611. bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[0] + l->toc_index, buf);
  1612. /* Write this stub out. */
  1613. if (! bfd_set_section_contents (outbfd,
  1614. bfd_get_section (l->start),
  1615. buf,
  1616. l->start->value,
  1617. POWERPC_STUB_SIZE))
  1618. bfd_fatal (_("writing stub"));
  1619. /* Create a new reloc for the TOC entry. */
  1620. reloc = (arelent *) xmalloc (sizeof (arelent));
  1621. reloc->sym_ptr_ptr = &l->reloc;
  1622. reloc->address = l->toc_index + got_sec->output_offset;
  1623. reloc->addend = 0;
  1624. reloc->howto = bfd_reloc_type_lookup (inbfd, BFD_RELOC_32);
  1625. *r++ = reloc;
  1626. }
  1627. bfd_set_reloc (outbfd, got_sec->output_section, relocs, stubcount);
  1628. }
  1629. /* Adjust relocation entries for PowerPC NetWare. We do not output
  1630. TOC relocations. The object code already contains the offset from
  1631. the TOC pointer. When the function is called, the TOC register,
  1632. r2, will be set to the correct TOC value, so there is no need for
  1633. any further reloc. */
  1634. static void
  1635. powerpc_mangle_relocs (bfd *outbfd, asection *insec,
  1636. arelent ***relocs_ptr,
  1637. long *reloc_count_ptr, char *contents,
  1638. bfd_size_type contents_size ATTRIBUTE_UNUSED)
  1639. {
  1640. reloc_howto_type *toc_howto;
  1641. long reloc_count;
  1642. arelent **relocs;
  1643. long i;
  1644. toc_howto = bfd_reloc_type_lookup (insec->owner, BFD_RELOC_PPC_TOC16);
  1645. if (toc_howto == (reloc_howto_type *) NULL)
  1646. fatal (_("Unable to locate PPC_TOC16 reloc information"));
  1647. /* If this is the .got section, clear out all the contents beyond
  1648. the initial size. We must do this here because copy_sections is
  1649. going to write out whatever we return in the contents field. */
  1650. if (strcmp (bfd_get_section_name (insec->owner, insec), ".got") == 0)
  1651. memset (contents + powerpc_initial_got_size, 0,
  1652. (size_t) (bfd_get_section_size (insec) - powerpc_initial_got_size));
  1653. reloc_count = *reloc_count_ptr;
  1654. relocs = *relocs_ptr;
  1655. for (i = 0; i < reloc_count; i++)
  1656. {
  1657. arelent *rel;
  1658. asymbol *sym;
  1659. bfd_vma sym_value;
  1660. rel = *relocs++;
  1661. sym = *rel->sym_ptr_ptr;
  1662. /* Convert any relocs against the .bss section into relocs
  1663. against the .data section. */
  1664. if (strcmp (bfd_get_section_name (outbfd, bfd_get_section (sym)),
  1665. NLM_UNINITIALIZED_DATA_NAME) == 0)
  1666. {
  1667. asection *datasec;
  1668. datasec = bfd_get_section_by_name (outbfd,
  1669. NLM_INITIALIZED_DATA_NAME);
  1670. if (datasec != NULL)
  1671. {
  1672. rel->addend += (bfd_get_section_vma (outbfd,
  1673. bfd_get_section (sym))
  1674. + sym->value);
  1675. rel->sym_ptr_ptr = datasec->symbol_ptr_ptr;
  1676. sym = *rel->sym_ptr_ptr;
  1677. }
  1678. }
  1679. /* PR 17512: file: 70cfde95. */
  1680. if (rel->howto == NULL)
  1681. continue;
  1682. /* We must be able to resolve all PC relative relocs at this
  1683. point. If we get a branch to an undefined symbol we build a
  1684. stub, since NetWare will resolve undefined symbols into a
  1685. pointer to a function descriptor. */
  1686. if (rel->howto->pc_relative)
  1687. {
  1688. /* This check for whether a symbol is in the same section as
  1689. the reloc will be wrong if there is a PC relative reloc
  1690. between two sections both of which were placed in the
  1691. same output section. This should not happen. */
  1692. if (bfd_get_section (sym) != insec->output_section)
  1693. non_fatal (_("unresolved PC relative reloc against %s"),
  1694. bfd_asymbol_name (sym));
  1695. else
  1696. {
  1697. bfd_vma val;
  1698. if (rel->address > contents_size - 4)
  1699. {
  1700. non_fatal (_("Out of range relocation: %lx"),
  1701. (long) rel->address);
  1702. break;
  1703. }
  1704. assert (rel->howto->size == 2 && rel->howto->pcrel_offset);
  1705. val = bfd_get_32 (outbfd, (bfd_byte *) contents + rel->address);
  1706. val = ((val &~ rel->howto->dst_mask)
  1707. | (((val & rel->howto->src_mask)
  1708. + (sym->value - rel->address)
  1709. + rel->addend)
  1710. & rel->howto->dst_mask));
  1711. bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
  1712. /* If this reloc is against an stubbed symbol and the
  1713. next instruction is
  1714. cror 31,31,31
  1715. then we replace the next instruction with
  1716. lwz r2,20(r1)
  1717. This reloads the TOC pointer after a stub call. */
  1718. if (bfd_asymbol_name (sym)[0] == '.'
  1719. && (sym->flags & BSF_DYNAMIC) != 0
  1720. && (bfd_get_32 (outbfd,
  1721. (bfd_byte *) contents + rel->address + 4)
  1722. == 0x4ffffb82)) /* cror 31,31,31 */
  1723. bfd_put_32 (outbfd, (bfd_vma) 0x80410014, /* lwz r2,20(r1) */
  1724. (bfd_byte *) contents + rel->address + 4);
  1725. --*reloc_count_ptr;
  1726. --relocs;
  1727. memmove (relocs, relocs + 1,
  1728. (size_t) ((reloc_count - 1) * sizeof (arelent *)));
  1729. continue;
  1730. }
  1731. }
  1732. /* When considering a TOC reloc, we do not want to include the
  1733. symbol value. The symbol will be start of the TOC section
  1734. (which is named .got). We do want to include the addend. */
  1735. if (rel->howto == toc_howto)
  1736. sym_value = 0;
  1737. else
  1738. sym_value = sym->value;
  1739. /* If this is a relocation against a symbol with a value, or
  1740. there is a reloc addend, we need to update the addend in the
  1741. object file. */
  1742. if (sym_value + rel->addend != 0)
  1743. {
  1744. bfd_vma val;
  1745. switch (rel->howto->size)
  1746. {
  1747. case 1:
  1748. if (rel->address > contents_size - 2)
  1749. {
  1750. non_fatal (_("Out of range relocation: %lx"),
  1751. (long) rel->address);
  1752. break;
  1753. }
  1754. val = bfd_get_16 (outbfd,
  1755. (bfd_byte *) contents + rel->address);
  1756. val = ((val &~ rel->howto->dst_mask)
  1757. | (((val & rel->howto->src_mask)
  1758. + sym_value
  1759. + rel->addend)
  1760. & rel->howto->dst_mask));
  1761. if ((bfd_signed_vma) val < - 0x8000
  1762. || (bfd_signed_vma) val >= 0x8000)
  1763. non_fatal (_("overflow when adjusting relocation against %s"),
  1764. bfd_asymbol_name (sym));
  1765. bfd_put_16 (outbfd, val, (bfd_byte *) contents + rel->address);
  1766. break;
  1767. case 2:
  1768. /* PR 17512: file: 0455a112. */
  1769. if (rel->address > contents_size - 4)
  1770. {
  1771. non_fatal (_("Out of range relocation: %lx"),
  1772. (long) rel->address);
  1773. break;
  1774. }
  1775. val = bfd_get_32 (outbfd,
  1776. (bfd_byte *) contents + rel->address);
  1777. val = ((val &~ rel->howto->dst_mask)
  1778. | (((val & rel->howto->src_mask)
  1779. + sym_value
  1780. + rel->addend)
  1781. & rel->howto->dst_mask));
  1782. bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
  1783. break;
  1784. default:
  1785. fatal (_("Unsupported relocation size: %d"), rel->howto->size);
  1786. }
  1787. if (! bfd_is_und_section (bfd_get_section (sym)))
  1788. rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
  1789. rel->addend = 0;
  1790. }
  1791. /* Now that we have incorporated the addend, remove any TOC
  1792. relocs. */
  1793. if (rel->howto == toc_howto)
  1794. {
  1795. --*reloc_count_ptr;
  1796. --relocs;
  1797. memmove (relocs, relocs + 1,
  1798. (size_t) ((reloc_count - i) * sizeof (arelent *)));
  1799. continue;
  1800. }
  1801. rel->address += insec->output_offset;
  1802. }
  1803. }
  1804. #endif /* NLMCONV_POWERPC */
  1805. /* Name of linker. */
  1806. #ifndef LD_NAME
  1807. #define LD_NAME "ld"
  1808. #endif
  1809. /* The user has specified several input files. Invoke the linker to
  1810. link them all together, and convert and delete the resulting output
  1811. file. */
  1812. static char *
  1813. link_inputs (struct string_list *inputs, char *ld, char * mfile)
  1814. {
  1815. size_t c;
  1816. struct string_list *q;
  1817. char **argv;
  1818. size_t i;
  1819. int pid;
  1820. int status;
  1821. char *errfmt;
  1822. char *errarg;
  1823. c = 0;
  1824. for (q = inputs; q != NULL; q = q->next)
  1825. ++c;
  1826. argv = (char **) xmalloc ((c + 7) * sizeof (char *));
  1827. #ifndef __MSDOS__
  1828. if (ld == NULL)
  1829. {
  1830. char *p;
  1831. /* Find the linker to invoke based on how nlmconv was run. */
  1832. p = program_name + strlen (program_name);
  1833. while (p != program_name)
  1834. {
  1835. if (p[-1] == '/')
  1836. {
  1837. ld = (char *) xmalloc (p - program_name + strlen (LD_NAME) + 1);
  1838. memcpy (ld, program_name, p - program_name);
  1839. strcpy (ld + (p - program_name), LD_NAME);
  1840. break;
  1841. }
  1842. --p;
  1843. }
  1844. }
  1845. #endif
  1846. if (ld == NULL)
  1847. ld = (char *) LD_NAME;
  1848. unlink_on_exit = make_temp_file (".O");
  1849. argv[0] = ld;
  1850. argv[1] = (char *) "-Ur";
  1851. argv[2] = (char *) "-o";
  1852. argv[3] = unlink_on_exit;
  1853. /* If we have been given the name of a mapfile and that
  1854. name is not 'stderr' then pass it on to the linker. */
  1855. if (mfile
  1856. && * mfile
  1857. && strcmp (mfile, "stderr") == 0)
  1858. {
  1859. argv[4] = (char *) "-Map";
  1860. argv[5] = mfile;
  1861. i = 6;
  1862. }
  1863. else
  1864. i = 4;
  1865. for (q = inputs; q != NULL; q = q->next, i++)
  1866. argv[i] = q->string;
  1867. argv[i] = NULL;
  1868. if (debug)
  1869. {
  1870. for (i = 0; argv[i] != NULL; i++)
  1871. fprintf (stderr, " %s", argv[i]);
  1872. fprintf (stderr, "\n");
  1873. }
  1874. pid = pexecute (ld, argv, program_name, (char *) NULL, &errfmt, &errarg,
  1875. PEXECUTE_SEARCH | PEXECUTE_ONE);
  1876. free (argv);
  1877. if (pid == -1)
  1878. {
  1879. fprintf (stderr, _("%s: execution of %s failed: "), program_name, ld);
  1880. fprintf (stderr, errfmt, errarg);
  1881. unlink (unlink_on_exit);
  1882. exit (1);
  1883. }
  1884. if (pwait (pid, &status, 0) < 0)
  1885. {
  1886. perror ("pwait");
  1887. unlink (unlink_on_exit);
  1888. exit (1);
  1889. }
  1890. if (status != 0)
  1891. {
  1892. non_fatal (_("Execution of %s failed"), ld);
  1893. unlink (unlink_on_exit);
  1894. exit (1);
  1895. }
  1896. return unlink_on_exit;
  1897. }