0008-remove_BFD_VMA_FMT.patch 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  1. diff -Nur elf2flt-v2021.08.orig/elf2flt.c elf2flt-v2021.08/elf2flt.c
  2. --- elf2flt-v2021.08.orig/elf2flt.c 2023-01-29 16:47:24.791851890 +0100
  3. +++ elf2flt-v2021.08/elf2flt.c 2023-01-29 16:49:46.628476326 +0100
  4. @@ -222,7 +222,7 @@
  5. long i;
  6. printf("SYMBOL TABLE:\n");
  7. for (i=0; i<number_of_symbols; i++) {
  8. - printf(" NAME=%s VALUE=0x%"BFD_VMA_FMT"x\n",
  9. + printf(" NAME=%s VALUE=0x%x\n",
  10. symbol_table[i]->name, symbol_table[i]->value);
  11. }
  12. printf("\n");
  13. @@ -471,7 +471,7 @@
  14. if (r == NULL)
  15. continue;
  16. if (verbose)
  17. - printf(" RELOCS: %s [%p]: flags=0x%x vma=0x%"BFD_VMA_FMT"x\n",
  18. + printf(" RELOCS: %s [%p]: flags=0x%x vma=0x%x\n",
  19. r->name, r, r->flags, elf2flt_bfd_section_vma(r));
  20. if ((r->flags & SEC_RELOC) == 0)
  21. continue;
  22. @@ -966,8 +966,8 @@
  23. if (verbose)
  24. fprintf(stderr,
  25. "%s vma=0x%x, "
  26. - "value=0x%"BFD_VMA_FMT"x, "
  27. - "address=0x%"BFD_VMA_FMT"x "
  28. + "value=0x%x, "
  29. + "address=0x%x "
  30. "sym_addr=0x%x rs=0x%x, opcode=0x%x\n",
  31. "ABS32",
  32. sym_vma, (*(q->sym_ptr_ptr))->value,
  33. @@ -985,8 +985,8 @@
  34. if (verbose)
  35. fprintf(stderr,
  36. "%s vma=0x%x, "
  37. - "value=0x%"BFD_VMA_FMT"x, "
  38. - "address=0x%"BFD_VMA_FMT"x "
  39. + "value=0x%x, "
  40. + "address=0x%x "
  41. "sym_addr=0x%x rs=0x%x, opcode=0x%x\n",
  42. "PLT32",
  43. sym_vma, (*(q->sym_ptr_ptr))->value,
  44. @@ -1008,7 +1008,7 @@
  45. case R_V850_ZDA_16_16_OFFSET:
  46. case R_V850_ZDA_16_16_SPLIT_OFFSET:
  47. /* Can't support zero-relocations. */
  48. - printf ("ERROR: %s+0x%"BFD_VMA_FMT"x: zero relocations not supported\n",
  49. + printf ("ERROR: %s+0x%x: zero relocations not supported\n",
  50. sym_name, q->addend);
  51. continue;
  52. #endif /* TARGET_v850 */
  53. @@ -1208,9 +1208,9 @@
  54. temp |= (exist_val & 0x3f);
  55. *(unsigned long *)r_mem = htoniosl(temp);
  56. if (verbose)
  57. - printf("omit: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  58. + printf("omit: offset=0x%x symbol=%s%s "
  59. "section=%s size=%d "
  60. - "fixup=0x%x (reloc=0x%"BFD_VMA_FMT"x) GPREL\n",
  61. + "fixup=0x%x (reloc=0x%x) GPREL\n",
  62. q->address, sym_name, addstr,
  63. section_name, sym_reloc_size,
  64. sym_addr, section_vma + q->address);
  65. @@ -1228,9 +1228,9 @@
  66. exist_val |= ((sym_addr & 0xFFFF) << 6);
  67. *(unsigned long *)r_mem = htoniosl(exist_val);
  68. if (verbose)
  69. - printf("omit: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  70. + printf("omit: offset=0x%x symbol=%s%s "
  71. "section=%s size=%d "
  72. - "fixup=0x%x (reloc=0x%"BFD_VMA_FMT"x) PCREL\n",
  73. + "fixup=0x%x (reloc=0x%x) PCREL\n",
  74. q->address, sym_name, addstr,
  75. section_name, sym_reloc_size,
  76. sym_addr, section_vma + q->address);
  77. @@ -1245,7 +1245,7 @@
  78. && (p[-1]->sym_ptr_ptr == p[0]->sym_ptr_ptr)
  79. && (p[-1]->addend == p[0]->addend)) {
  80. if (verbose)
  81. - printf("omit: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  82. + printf("omit: offset=0x%x symbol=%s%s "
  83. "section=%s size=%d LO16\n",
  84. q->address, sym_name, addstr,
  85. section_name, sym_reloc_size);
  86. @@ -1660,9 +1660,9 @@
  87. */
  88. if (relocation_needed) {
  89. if (verbose)
  90. - printf(" RELOC[%d]: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  91. + printf(" RELOC[%d]: offset=0x%x symbol=%s%s "
  92. "section=%s size=%d "
  93. - "fixup=0x%x (reloc=0x%"BFD_VMA_FMT"x)\n",
  94. + "fixup=0x%x (reloc=0x%x)\n",
  95. flat_reloc_count,
  96. q->address, sym_name, addstr,
  97. section_name, sym_reloc_size,
  98. diff -Nur elf2flt-v2021.08.orig/elf2flt.c.orig elf2flt-v2021.08/elf2flt.c.orig
  99. --- elf2flt-v2021.08.orig/elf2flt.c.orig 2023-01-29 16:47:18.264007463 +0100
  100. +++ elf2flt-v2021.08/elf2flt.c.orig 1970-01-01 01:00:00.000000000 +0100
  101. @@ -1,2113 +0,0 @@
  102. -/*
  103. - * elf2flt.c: Convert ELF (or any BFD format) to FLAT binary format
  104. - *
  105. - * (c) 1999-2002, Greg Ungerer <gerg@snapgear.com>
  106. - * Created elf2flt from coff2flt (see copyrights below). Added all the
  107. - * ELF format file handling. Extended relocation support for all of
  108. - * text and data.
  109. - *
  110. - * (c) 2008-2009, Xtensa support, Oskar Schirmer <os@emlix.com>
  111. - * (c) 2006 Support the -a (use_resolved) option for TARGET_arm.
  112. - * Shaun Jackman <sjackman@gmail.com>
  113. - * (c) 2004, Nios II support, Wentao Xu <wentao@microtronix.com>
  114. - * (c) 2003, H8 support, ktrace <davidm@snapgear.com>
  115. - * (c) 2003-2004, MicroBlaze support, John Williams <jwilliams@itee.uq.edu.au>
  116. - * (c) 2001-2003, arm/arm-pic/arm-big-endian support <davidm@snapgear.com>
  117. - * (c) 2001, v850 changes, Mile Bader <miles@lsi.nec.co.jp>
  118. - * (c) 2003, SuperH support, Paul Mundt <lethal@linux-sh.org>
  119. - * (c) 2001, zflat support <davidm@snapgear.com>
  120. - * (c) 2001, Changes for GOT entries Paul Dale <pauli@snapgear.com> and
  121. - * David McCullough <davidm@snapgear.com>
  122. - *
  123. - * Now supports PIC with GOT tables. This works by taking a '.elf' file
  124. - * and a fully linked elf executable (at address 0) and produces a flat
  125. - * file that can be loaded with some fixups. It still supports the old
  126. - * style fully relocatable elf format files.
  127. - *
  128. - * Originally obj-res.c
  129. - *
  130. - * (c) 1998, Kenneth Albanowski <kjahds@kjahds.com>
  131. - * (c) 1998, D. Jeff Dionne
  132. - * (c) 1998, The Silver Hammer Group Ltd.
  133. - * (c) 1996, 1997 Dionne & Associates <jeff@ryeham.ee.ryerson.ca>
  134. - *
  135. - * This is Free Software, under the GNU Public Licence v2 or greater.
  136. - *
  137. - * Relocation added March 1997, Kresten Krab Thorup
  138. - * krab@california.daimi.aau.dk
  139. - */
  140. -
  141. -#include <inttypes.h> /* All the standard PRI define times for printf */
  142. -#include <stdio.h> /* Userland pieces of the ANSI C standard I/O package */
  143. -#include <stdlib.h> /* Userland prototypes of the ANSI C std lib functions */
  144. -#include <stdarg.h> /* Allows va_list to exist in the these namespaces */
  145. -#include <string.h> /* Userland prototypes of the string handling funcs */
  146. -#include <strings.h>
  147. -#include <unistd.h> /* Userland prototypes of the Unix std system calls */
  148. -#include <fcntl.h> /* Flag value for file handling functions */
  149. -#include <time.h>
  150. -#include <errno.h>
  151. -
  152. -/* from $(INSTALLDIR)/include */
  153. -#include <bfd.h> /* Main header file for the BFD library */
  154. -#include <libiberty.h>
  155. -
  156. -#include "stubs.h"
  157. -const char *elf2flt_progname;
  158. -
  159. -/* Include the right header file for the R_xxx defines. */
  160. -#if defined(TARGET_arm)
  161. -#include <elf/arm.h>
  162. -#elif defined(TARGET_bfin)
  163. -#include <elf/bfin.h>
  164. -#elif defined(TARGET_h8300)
  165. -#include <elf/h8.h>
  166. -#elif defined(TARGET_m68k)
  167. -#include <elf/m68k.h>
  168. -#elif defined(TARGET_microblaze)
  169. -#include <elf/microblaze.h>
  170. -#elif defined(TARGET_nios) || defined(TARGET_nios2)
  171. -/* Altera NIOS specific definitions. */
  172. -#define FLAT_NIOS2_R_32 0 /* Normal 32-bit reloc */
  173. -#define FLAT_NIOS2_R_HI_LO 1
  174. -#define FLAT_NIOS2_R_HIADJ_LO 2
  175. -#define FLAT_NIOS2_R_CALL26 4
  176. -#include <elf/nios2.h>
  177. -#elif defined(TARGET_sh)
  178. -#include <elf/sh.h>
  179. -#elif defined(TARGET_sparc)
  180. -#include <elf/sparc.h>
  181. -#elif defined(TARGET_v850)
  182. -#include <elf/v850.h>
  183. -#elif defined(TARGET_xtensa)
  184. -#include <elf/xtensa.h>
  185. -#elif defined(TARGET_riscv64)
  186. -#include <elf/riscv.h>
  187. -#endif
  188. -
  189. -#if defined(__MINGW32__)
  190. -#include <getopt.h>
  191. -#endif
  192. -
  193. -/* from uClinux-x.x.x/include/linux */
  194. -#include "flat.h" /* Binary flat header description */
  195. -#include "compress.h"
  196. -
  197. -#ifdef TARGET_e1
  198. -#include <e1.h>
  199. -#endif
  200. -
  201. -#ifdef TARGET_v850e
  202. -#define TARGET_v850
  203. -#endif
  204. -
  205. -#if defined(TARGET_m68k)
  206. -#define ARCH "m68k/coldfire"
  207. -#elif defined(TARGET_arm)
  208. -#define ARCH "arm"
  209. -#elif defined(TARGET_sparc)
  210. -#define ARCH "sparc"
  211. -#elif defined(TARGET_v850)
  212. -#define ARCH "v850"
  213. -#elif defined(TARGET_sh)
  214. -#define ARCH "sh"
  215. -#elif defined(TARGET_h8300)
  216. -#define ARCH "h8300"
  217. -#elif defined(TARGET_microblaze)
  218. -#define ARCH "microblaze"
  219. -#elif defined(TARGET_e1)
  220. -#define ARCH "e1-coff"
  221. -#elif defined(TARGET_bfin)
  222. -#define ARCH "bfin"
  223. -#elif defined(TARGET_nios)
  224. -#define ARCH "nios"
  225. -#elif defined(TARGET_nios2)
  226. -#define ARCH "nios2"
  227. -#elif defined(TARGET_xtensa)
  228. -#define ARCH "xtensa"
  229. -#elif defined(TARGET_riscv64)
  230. -#define ARCH "riscv64"
  231. -#else
  232. -#error "Don't know how to support your CPU architecture??"
  233. -#endif
  234. -
  235. -#if defined(TARGET_m68k) || defined(TARGET_h8300) || defined(TARGET_bfin)
  236. -/*
  237. - * Define a maximum number of bytes allowed in the offset table.
  238. - * We'll fail if the table is larger than this.
  239. - *
  240. - * This limit may be different for platforms other than m68k, but
  241. - * 8000 entries is a lot, trust me :-) (davidm)
  242. - */
  243. -#define GOT_LIMIT 32767
  244. -/*
  245. - * we have to mask out the shared library id here and there, this gives
  246. - * us the real address bits when needed
  247. - */
  248. -#define real_address_bits(x) (pic_with_got ? ((x) & 0xffffff) : (x))
  249. -#else
  250. -#define real_address_bits(x) (x)
  251. -#endif
  252. -
  253. -#ifndef O_BINARY
  254. -#define O_BINARY 0
  255. -#endif
  256. -
  257. -/*
  258. - * The bfd parameter isn't actually used by any of the bfd_section funcs and
  259. - * have been removed since binutils 2.34.
  260. - */
  261. -#ifdef HAVE_BFD_SECTION_API_TAKES_BFD
  262. -#define elf2flt_bfd_section_size(s) bfd_section_size(NULL, s)
  263. -#define elf2flt_bfd_section_vma(s) bfd_section_vma(NULL, s)
  264. -#else
  265. -#define elf2flt_bfd_section_size(s) bfd_section_size(s)
  266. -#define elf2flt_bfd_section_vma(s) bfd_section_vma(s)
  267. -#endif
  268. -
  269. -/* Extra output when running. */
  270. -static int verbose = 0;
  271. -/* Do ELF/GOT processing with PIC code. */
  272. -static int pic_with_got = 0;
  273. -/* Instruct loader to allocate everything into RAM. */
  274. -static int load_to_ram = 0;
  275. -/* Instruct kernel loader to output debug/trace info when loading. */
  276. -static int ktrace = 0;
  277. -/* 1 = compress everything, 2 = compress data only. */
  278. -static int docompress = 0;
  279. -/* If true, get the value of symbol references from the program contents,
  280. - not from the relocation table. In this case, the input ELF file must
  281. - be already fully resolved (using the `-q' flag with recent versions of
  282. - GNU ld will give you a fully resolved output file with relocation
  283. - entries). */
  284. -static int use_resolved = 0;
  285. -
  286. -/* Set if the text section contains any relocations. If it does, we must
  287. - set the load_to_ram flag. */
  288. -static int text_has_relocs = 0;
  289. -
  290. -static asymbol **
  291. -get_symbols (bfd *abfd, long *num)
  292. -{
  293. - int32_t storage_needed;
  294. - asymbol **symbol_table;
  295. - long number_of_symbols;
  296. -
  297. - storage_needed = bfd_get_symtab_upper_bound (abfd);
  298. -
  299. - if (storage_needed < 0)
  300. - abort ();
  301. -
  302. - if (storage_needed == 0)
  303. - return NULL;
  304. -
  305. - symbol_table = xmalloc (storage_needed);
  306. -
  307. - number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
  308. -
  309. - if (number_of_symbols < 0)
  310. - abort ();
  311. -
  312. - *num = number_of_symbols;
  313. - return symbol_table;
  314. -}
  315. -
  316. -
  317. -
  318. -static int
  319. -dump_symbols(asymbol **symbol_table, long number_of_symbols)
  320. -{
  321. - long i;
  322. - printf("SYMBOL TABLE:\n");
  323. - for (i=0; i<number_of_symbols; i++) {
  324. - printf(" NAME=%s VALUE=0x%"BFD_VMA_FMT"x\n",
  325. - symbol_table[i]->name, symbol_table[i]->value);
  326. - }
  327. - printf("\n");
  328. - return(0);
  329. -}
  330. -
  331. -
  332. -
  333. -#if !defined TARGET_e1 && !defined TARGET_bfin
  334. -static long
  335. -get_symbol_offset(char *name, asection *sec, asymbol **symbol_table, long number_of_symbols)
  336. -{
  337. - long i;
  338. - for (i=0; i<number_of_symbols; i++) {
  339. - if (symbol_table[i]->section == sec) {
  340. - if (!strcmp(symbol_table[i]->name, name)) {
  341. - return symbol_table[i]->value;
  342. - }
  343. - }
  344. - }
  345. - return -1;
  346. -}
  347. -#endif
  348. -
  349. -
  350. -
  351. -#ifdef TARGET_nios2
  352. -static long
  353. -get_gp_value(asymbol **symbol_table, long number_of_symbols)
  354. -{
  355. - long i;
  356. - for (i=0; i<number_of_symbols; i++) {
  357. - if (!strcmp(symbol_table[i]->name, "_gp"))
  358. - return symbol_table[i]->value;
  359. - }
  360. - return -1;
  361. -}
  362. -#endif
  363. -
  364. -
  365. -
  366. -static int32_t
  367. -add_com_to_bss(asymbol **symbol_table, int32_t number_of_symbols, int32_t bss_len)
  368. -{
  369. - int32_t i, comsize;
  370. - int32_t offset;
  371. -
  372. - comsize = 0;
  373. - for (i=0; i<number_of_symbols; i++) {
  374. - if (strcmp("*COM*", symbol_table[i]->section->name) == 0) {
  375. - offset = bss_len + comsize;
  376. - comsize += symbol_table[i]->value;
  377. - symbol_table[i]->value = offset;
  378. - }
  379. - }
  380. - return comsize;
  381. -}
  382. -
  383. -#ifdef TARGET_bfin
  384. -/* FUNCTION : weak_und_symbol
  385. - ABSTRACT : return true if symbol is weak and undefined.
  386. -*/
  387. -static int
  388. -weak_und_symbol(const char *reloc_section_name,
  389. - struct bfd_symbol *symbol)
  390. -{
  391. - if (!(strstr (reloc_section_name, "text")
  392. - || strstr (reloc_section_name, "data")
  393. - || strstr (reloc_section_name, "bss"))) {
  394. - if (symbol->flags & BSF_WEAK) {
  395. -#ifdef DEBUG_BFIN
  396. - fprintf(stderr, "found weak undefined symbol %s\n", symbol->name);
  397. -#endif
  398. - return TRUE;
  399. - }
  400. - }
  401. - return FALSE;
  402. -}
  403. -
  404. -static int
  405. -bfin_set_reloc (uint32_t *reloc,
  406. - const char *reloc_section_name,
  407. - const char *sym_name,
  408. - struct bfd_symbol *symbol,
  409. - int sp, int32_t offset)
  410. -{
  411. - unsigned int type = 0;
  412. - uint32_t val;
  413. -
  414. - if (strstr (reloc_section_name, "stack")) {
  415. - if (verbose)
  416. - printf("Stack-relative reloc, offset %08"PRIx32"\n", offset);
  417. - /* This must be a stack_start reloc for stack checking. */
  418. - type = 1;
  419. - }
  420. - val = (offset & ((1 << 26) - 1));
  421. - val |= (sp & ((1 << 3) - 1)) << 26;
  422. - val |= type << 29;
  423. - *reloc = val;
  424. - return 0;
  425. -}
  426. -
  427. -static bfd *compare_relocs_bfd;
  428. -
  429. -static int
  430. -compare_relocs (const void *pa, const void *pb)
  431. -{
  432. - const arelent *const *a = pa, *const *b = pb;
  433. - const arelent *ra = *a, *rb = *b;
  434. - unsigned long va, vb;
  435. - uint32_t a_vma, b_vma;
  436. -
  437. - if (!ra->sym_ptr_ptr || !*ra->sym_ptr_ptr)
  438. - return -1;
  439. - else if (!rb->sym_ptr_ptr || !*rb->sym_ptr_ptr)
  440. - return 1;
  441. -
  442. - a_vma = elf2flt_bfd_section_vma((*(ra->sym_ptr_ptr))->section);
  443. - b_vma = elf2flt_bfd_section_vma((*(rb->sym_ptr_ptr))->section);
  444. - va = (*(ra->sym_ptr_ptr))->value + a_vma + ra->addend;
  445. - vb = (*(rb->sym_ptr_ptr))->value + b_vma + rb->addend;
  446. - return va - vb;
  447. -}
  448. -#endif
  449. -
  450. -static bool
  451. -ro_reloc_data_section_should_be_in_text(asection *s)
  452. -{
  453. - if ((s->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) ==
  454. - (SEC_DATA | SEC_READONLY | SEC_RELOC)) {
  455. -#if defined(TARGET_m68k) || defined(TARGET_riscv64) || defined(TARGET_xtensa)
  456. - if (!strcmp(".eh_frame", s->name))
  457. - return false;
  458. -#endif
  459. - return true;
  460. - }
  461. - return false;
  462. -}
  463. -
  464. -static uint32_t *
  465. -output_relocs (
  466. - bfd *abs_bfd,
  467. - asymbol **symbols,
  468. - int number_of_symbols,
  469. - uint32_t *n_relocs,
  470. - unsigned char *text, int text_len, uint32_t text_vma,
  471. - unsigned char *data, int data_len, uint32_t data_vma,
  472. - bfd *rel_bfd)
  473. -{
  474. - uint32_t *flat_relocs;
  475. - asection *a, *sym_section, *r;
  476. - arelent **relpp, **p, *q;
  477. - const char *sym_name, *section_name;
  478. - unsigned char *sectionp;
  479. - unsigned long pflags;
  480. - char addstr[20];
  481. - uint32_t sym_addr, sym_vma, section_vma;
  482. - int relsize, relcount;
  483. - int flat_reloc_count;
  484. - int sym_reloc_size, rc;
  485. - int got_size = 0;
  486. - int bad_relocs = 0;
  487. - asymbol **symb;
  488. - long nsymb;
  489. -#ifdef TARGET_bfin
  490. - unsigned long persistent_data = 0;
  491. -#endif
  492. -
  493. -#if 0
  494. - printf("%s(%d): output_relocs(abs_bfd=%d,symbols=0x%x,number_of_symbols=%d,"
  495. - "n_relocs=0x%x,text=0x%x,text_len=%d,data=0x%x,data_len=%d)\n",
  496. - __FILE__, __LINE__, abs_bfd, symbols, number_of_symbols, n_relocs,
  497. - text, text_len, data, data_len);
  498. -#endif
  499. -
  500. - if (0)
  501. - dump_symbols(symbols, number_of_symbols);
  502. -
  503. - *n_relocs = 0;
  504. - flat_relocs = NULL;
  505. - flat_reloc_count = 0;
  506. - sym_reloc_size = 0;
  507. - rc = 0;
  508. - pflags = 0;
  509. - /* Silence gcc warnings */
  510. - (void) pflags;
  511. - (void) sym_vma;
  512. -
  513. - /* Determine how big our offset table is in bytes.
  514. - * This isn't too difficult as we've terminated the table with -1.
  515. - * Also note that both the relocatable and absolute versions have this
  516. - * terminator even though the relocatable one doesn't have the GOT!
  517. - */
  518. - if (pic_with_got && !use_resolved) {
  519. - uint32_t *lp = (uint32_t *)data;
  520. - /* Should call ntohl(*lp) here but is isn't going to matter */
  521. - while (*lp != 0xffffffff) lp++;
  522. - got_size = ((unsigned char *)lp) - data;
  523. - if (verbose)
  524. - printf("GOT table contains %zu entries (%d bytes)\n",
  525. - got_size/sizeof(uint32_t), got_size);
  526. -#ifdef TARGET_m68k
  527. - if (got_size > GOT_LIMIT)
  528. - fatal("GOT too large: %d bytes (limit = %d bytes)",
  529. - got_size, GOT_LIMIT);
  530. -#endif
  531. - }
  532. -
  533. - for (a = abs_bfd->sections; (a != (asection *) NULL); a = a->next) {
  534. - section_vma = elf2flt_bfd_section_vma(a);
  535. -
  536. - if (verbose)
  537. - printf("SECTION: %s [%p]: flags=0x%x vma=0x%"PRIx32"\n",
  538. - a->name, a, a->flags, section_vma);
  539. -
  540. -// if (bfd_is_abs_section(a))
  541. -// continue;
  542. - if (bfd_is_und_section(a))
  543. - continue;
  544. - if (bfd_is_com_section(a))
  545. - continue;
  546. -// if ((a->flags & SEC_RELOC) == 0)
  547. -// continue;
  548. -
  549. - /*
  550. - * Only relocate things in the writable data sections if we are PIC/GOT.
  551. - * Otherwise do text (and read only data) as well.
  552. - */
  553. - if ((!pic_with_got || ALWAYS_RELOC_TEXT) &&
  554. - ((a->flags & SEC_CODE) ||
  555. - ro_reloc_data_section_should_be_in_text(a)))
  556. - sectionp = text + (a->vma - text_vma);
  557. - else if (a->flags & SEC_DATA)
  558. - sectionp = data + (a->vma - data_vma);
  559. - else
  560. - continue;
  561. -
  562. - /* Now search for the equivalent section in the relocation binary
  563. - * and use that relocation information to build reloc entries
  564. - * for this one.
  565. - */
  566. - for (r=rel_bfd->sections; r != NULL; r=r->next)
  567. - if (strcmp(a->name, r->name) == 0)
  568. - break;
  569. - if (r == NULL)
  570. - continue;
  571. - if (verbose)
  572. - printf(" RELOCS: %s [%p]: flags=0x%x vma=0x%"BFD_VMA_FMT"x\n",
  573. - r->name, r, r->flags, elf2flt_bfd_section_vma(r));
  574. - if ((r->flags & SEC_RELOC) == 0)
  575. - continue;
  576. - relsize = bfd_get_reloc_upper_bound(rel_bfd, r);
  577. - if (relsize <= 0) {
  578. - if (verbose)
  579. - printf("%s(%d): no relocation entries section=%s\n",
  580. - __FILE__, __LINE__, r->name);
  581. - continue;
  582. - }
  583. -
  584. - symb = get_symbols(rel_bfd, &nsymb);
  585. - relpp = xmalloc(relsize);
  586. -
  587. - relcount = bfd_canonicalize_reloc(rel_bfd, r, relpp, symb);
  588. - if (relcount <= 0) {
  589. - if (verbose)
  590. - printf("%s(%d): no relocation entries section=%s\n",
  591. - __FILE__, __LINE__, r->name);
  592. - continue;
  593. - } else {
  594. -#ifdef TARGET_bfin
  595. - compare_relocs_bfd = abs_bfd;
  596. - qsort (relpp, relcount, sizeof *relpp, compare_relocs);
  597. -#endif
  598. - for (p = relpp; (relcount && (*p != NULL)); p++, relcount--) {
  599. - unsigned char *r_mem = NULL;
  600. - int relocation_needed = 0;
  601. - /* Do we need to update the text segment? By default yes if not pic_with_got */
  602. - int update_text = !pic_with_got;
  603. -
  604. -#ifdef TARGET_v850
  605. - /* Skip this relocation entirely if possible (we
  606. - do this early, before doing any other
  607. - processing on it). */
  608. - switch ((*p)->howto->type) {
  609. - case R_V850_9_PCREL:
  610. - case R_V850_22_PCREL:
  611. - case R_V850_SDA_16_16_OFFSET:
  612. - case R_V850_SDA_15_16_OFFSET:
  613. - case R_V850_ZDA_15_16_OFFSET:
  614. - case R_V850_TDA_6_8_OFFSET:
  615. - case R_V850_TDA_7_8_OFFSET:
  616. - case R_V850_TDA_7_7_OFFSET:
  617. - case R_V850_TDA_16_16_OFFSET:
  618. - case R_V850_TDA_4_5_OFFSET:
  619. - case R_V850_TDA_4_4_OFFSET:
  620. - case R_V850_SDA_16_16_SPLIT_OFFSET:
  621. - case R_V850_CALLT_6_7_OFFSET:
  622. - case R_V850_CALLT_16_16_OFFSET:
  623. - /* These are relative relocations, which
  624. - have already been fixed up by the
  625. - linker at this point, so just ignore
  626. - them. */
  627. - continue;
  628. - }
  629. -#endif /* USE_V850_RELOCS */
  630. -
  631. - q = *p;
  632. - if (q->sym_ptr_ptr && *q->sym_ptr_ptr) {
  633. - sym_name = (*(q->sym_ptr_ptr))->name;
  634. - sym_section = (*(q->sym_ptr_ptr))->section;
  635. - section_name=(*(q->sym_ptr_ptr))->section->name;
  636. - } else {
  637. - printf("ERROR: undefined relocation entry\n");
  638. - rc = -1;
  639. - continue;
  640. - }
  641. -#ifndef TARGET_bfin
  642. - /* Adjust the address to account for the GOT table which wasn't
  643. - * present in the relative file link.
  644. - */
  645. - if (pic_with_got && !use_resolved)
  646. - q->address += got_size;
  647. -#endif
  648. -
  649. - /* A pointer to what's being relocated, used often
  650. - below. */
  651. - r_mem = sectionp + q->address;
  652. -
  653. - /*
  654. - * Fixup offset in the actual section.
  655. - */
  656. - addstr[0] = 0;
  657. -#if !defined TARGET_e1 && !defined TARGET_bfin
  658. - if ((sym_addr = get_symbol_offset((char *) sym_name,
  659. - sym_section, symbols, number_of_symbols)) == -1) {
  660. - sym_addr = 0;
  661. - }
  662. -#else
  663. - sym_addr = (*(q->sym_ptr_ptr))->value;
  664. -#endif
  665. - if (use_resolved) {
  666. - /* Use the address of the symbol already in
  667. - the program text. How this is handled may
  668. - still depend on the particular relocation
  669. - though. */
  670. - switch (q->howto->type) {
  671. -#ifdef TARGET_v850
  672. - int r2_type;
  673. - case R_V850_HI16_S:
  674. - /* We specially handle adjacent
  675. - HI16_S/ZDA_15_16_OFFSET and
  676. - HI16_S/LO16 pairs that reference the
  677. - same address (these are usually
  678. - movhi/ld and movhi/movea pairs,
  679. - respectively). */
  680. - if (relcount == 0)
  681. - r2_type = R_V850_NONE;
  682. - else
  683. - r2_type = p[1]->howto->type;
  684. - if ((r2_type == R_V850_ZDA_15_16_OFFSET
  685. - || r2_type == R_V850_LO16)
  686. - && (p[0]->sym_ptr_ptr
  687. - == p[1]->sym_ptr_ptr)
  688. - && (p[0]->addend == p[1]->addend))
  689. - {
  690. - relocation_needed = 1;
  691. -
  692. - switch (r2_type) {
  693. - case R_V850_ZDA_15_16_OFFSET:
  694. - pflags = 0x10000000;
  695. - break;
  696. - case R_V850_LO16:
  697. - pflags = 0x20000000;
  698. - break;
  699. - }
  700. -
  701. - /* We don't really need the
  702. - actual value -- the bits
  703. - produced by the linker are
  704. - what we want in the final
  705. - flat file -- but get it
  706. - anyway if useful for
  707. - debugging. */
  708. - if (verbose) {
  709. - unsigned char *r2_mem =
  710. - sectionp
  711. - + p[1]->address;
  712. - /* little-endian */
  713. - int hi = r_mem[0]
  714. - + (r_mem[1] << 8);
  715. - int lo = r2_mem[0]
  716. - + (r2_mem[1] << 8);
  717. - /* Sign extend LO. */
  718. - lo = (lo ^ 0x8000)
  719. - - 0x8000;
  720. -
  721. - /* Maybe ignore the LSB
  722. - of LO, which is
  723. - actually part of the
  724. - instruction. */
  725. - if (r2_type != R_V850_LO16)
  726. - lo &= ~1;
  727. -
  728. - sym_addr =
  729. - (hi << 16)
  730. - + lo;
  731. - }
  732. - } else
  733. - goto bad_resolved_reloc;
  734. - break;
  735. -
  736. - case R_V850_LO16:
  737. - /* See if this is actually the
  738. - 2nd half of a pair. */
  739. - if (p > relpp
  740. - && (p[-1]->howto->type
  741. - == R_V850_HI16_S)
  742. - && (p[-1]->sym_ptr_ptr
  743. - == p[0]->sym_ptr_ptr)
  744. - && (p[-1]->addend == p[0]->addend))
  745. - break; /* not an error */
  746. - else
  747. - goto bad_resolved_reloc;
  748. -
  749. - case R_V850_HI16:
  750. - goto bad_resolved_reloc;
  751. - default:
  752. - goto good_32bit_resolved_reloc;
  753. -#elif defined(TARGET_microblaze)
  754. - case R_MICROBLAZE_64:
  755. - sym_addr =
  756. - (r_mem[2] << 24)
  757. - + (r_mem[3] << 16)
  758. - + (r_mem[6] << 8)
  759. - + r_mem[7];
  760. - relocation_needed = 1;
  761. - update_text = 0;
  762. - pflags = 0x80000000;
  763. - break;
  764. - case R_MICROBLAZE_GOTPC_64:
  765. - /* This is data-relative. We can't support this with bflt */
  766. - goto bad_resolved_reloc;
  767. - case R_MICROBLAZE_GOT_64:
  768. - /* This is a GOT relocation. But it is accessed via the GOT register (r20)
  769. - * so doesn't need relocation
  770. - */
  771. - relocation_needed = 0;
  772. - break;
  773. - case R_MICROBLAZE_32:
  774. - goto good_32bit_resolved_reloc;
  775. - /* These are already relocated for us as text-relative, or are dummy entries */
  776. - case R_MICROBLAZE_64_PCREL:
  777. - case R_MICROBLAZE_PLT_64:
  778. - case R_MICROBLAZE_NONE:
  779. - case R_MICROBLAZE_64_NONE:
  780. - case R_MICROBLAZE_32_PCREL_LO:
  781. - relocation_needed = 0;
  782. - continue;
  783. - default:
  784. - goto bad_resolved_reloc;
  785. -#elif defined(TARGET_arm)
  786. - case R_ARM_TARGET1:
  787. - case R_ARM_TARGET2:
  788. - case R_ARM_ABS32:
  789. - relocation_needed = 1;
  790. - break;
  791. - case R_ARM_REL32:
  792. - case R_ARM_JUMP24:
  793. - case R_ARM_CALL:
  794. - case R_ARM_THM_PC11:
  795. - case R_ARM_THM_PC22:
  796. - case R_ARM_THM_JUMP24:
  797. - case R_ARM_PC24:
  798. - case R_ARM_PLT32:
  799. - case R_ARM_GOTPC:
  800. - case R_ARM_GOT32:
  801. - case R_ARM_PREL31:
  802. - case R_ARM_V4BX:
  803. - case R_ARM_NONE:
  804. - relocation_needed = 0;
  805. - break;
  806. - default:
  807. - goto bad_resolved_reloc;
  808. -#elif defined(TARGET_m68k)
  809. - case R_68K_32:
  810. - goto good_32bit_resolved_reloc;
  811. - case R_68K_PC32:
  812. - case R_68K_PC16:
  813. - /* The linker has already resolved
  814. - PC relocs for us. In PIC links,
  815. - the symbol must be in the data
  816. - segment. */
  817. - case R_68K_NONE:
  818. - continue;
  819. - default:
  820. - goto bad_resolved_reloc;
  821. -#elif defined TARGET_bfin
  822. - case R_BFIN_RIMM16:
  823. - case R_BFIN_LUIMM16:
  824. - case R_BFIN_HUIMM16:
  825. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  826. - sym_addr += sym_vma + q->addend;
  827. -
  828. - if (weak_und_symbol(sym_section->name, (*(q->sym_ptr_ptr))))
  829. - continue;
  830. - if (q->howto->type == R_BFIN_RIMM16 && (0xFFFF0000 & sym_addr)) {
  831. - fprintf (stderr, "Relocation overflow for rN = %s\n",sym_name);
  832. - bad_relocs++;
  833. - }
  834. - if ((0xFFFF0000 & sym_addr) != persistent_data) {
  835. - flat_relocs = (uint32_t *)
  836. - (realloc (flat_relocs, (flat_reloc_count + 1) * sizeof (uint32_t)));
  837. - if (verbose)
  838. - printf("New persistent data for %08"PRIx32"\n", sym_addr);
  839. - persistent_data = 0xFFFF0000 & sym_addr;
  840. - flat_relocs[flat_reloc_count++]
  841. - = (sym_addr >> 16) | (3 << 26);
  842. - }
  843. -
  844. - flat_relocs = (uint32_t *)
  845. - (realloc (flat_relocs, (flat_reloc_count + 1) * sizeof (uint32_t)));
  846. - if (bfin_set_reloc (flat_relocs + flat_reloc_count,
  847. - sym_section->name, sym_name,
  848. - (*(q->sym_ptr_ptr)),
  849. - q->howto->type == R_BFIN_HUIMM16 ? 1 : 0,
  850. - section_vma + q->address))
  851. - bad_relocs++;
  852. - if (a->flags & SEC_CODE)
  853. - text_has_relocs = 1;
  854. - flat_reloc_count++;
  855. - break;
  856. -
  857. - case R_BFIN_BYTE4_DATA:
  858. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  859. - sym_addr += sym_vma + q->addend;
  860. -
  861. - if (weak_und_symbol (sym_section->name, (*(q->sym_ptr_ptr))))
  862. - continue;
  863. -
  864. - flat_relocs = (uint32_t *)
  865. - (realloc (flat_relocs, (flat_reloc_count + 1) * sizeof (uint32_t)));
  866. - if (bfin_set_reloc (flat_relocs + flat_reloc_count,
  867. - sym_section->name, sym_name,
  868. - (*(q->sym_ptr_ptr)),
  869. - 2, section_vma + q->address))
  870. - bad_relocs++;
  871. - if (a->flags & SEC_CODE)
  872. - text_has_relocs = 1;
  873. -
  874. - flat_reloc_count++;
  875. - break;
  876. -#elif defined (TARGET_h8300)
  877. - case R_H8_DIR32:
  878. - case R_H8_DIR32A16:
  879. - case R_H8_DISP32A16:
  880. - r_mem[0] = 0;
  881. - case R_H8_DIR24A8:
  882. - case R_H8_DIR24R8:
  883. - goto good_32bit_resolved_reloc;
  884. - case R_H8_PCREL8:
  885. - case R_H8_PCREL16:
  886. - continue;
  887. -#elif defined(TARGET_xtensa)
  888. - case R_XTENSA_NONE:
  889. - case R_XTENSA_OP0:
  890. - case R_XTENSA_OP1:
  891. - case R_XTENSA_OP2:
  892. - case R_XTENSA_SLOT0_OP:
  893. - case R_XTENSA_SLOT1_OP:
  894. - case R_XTENSA_SLOT2_OP:
  895. - case R_XTENSA_SLOT3_OP:
  896. - case R_XTENSA_SLOT4_OP:
  897. - case R_XTENSA_SLOT5_OP:
  898. - case R_XTENSA_SLOT6_OP:
  899. - case R_XTENSA_SLOT7_OP:
  900. - case R_XTENSA_SLOT8_OP:
  901. - case R_XTENSA_SLOT9_OP:
  902. - case R_XTENSA_SLOT10_OP:
  903. - case R_XTENSA_SLOT11_OP:
  904. - case R_XTENSA_SLOT12_OP:
  905. - case R_XTENSA_SLOT13_OP:
  906. - case R_XTENSA_SLOT14_OP:
  907. - case R_XTENSA_SLOT0_ALT:
  908. - case R_XTENSA_SLOT1_ALT:
  909. - case R_XTENSA_SLOT2_ALT:
  910. - case R_XTENSA_SLOT3_ALT:
  911. - case R_XTENSA_SLOT4_ALT:
  912. - case R_XTENSA_SLOT5_ALT:
  913. - case R_XTENSA_SLOT6_ALT:
  914. - case R_XTENSA_SLOT7_ALT:
  915. - case R_XTENSA_SLOT8_ALT:
  916. - case R_XTENSA_SLOT9_ALT:
  917. - case R_XTENSA_SLOT10_ALT:
  918. - case R_XTENSA_SLOT11_ALT:
  919. - case R_XTENSA_SLOT12_ALT:
  920. - case R_XTENSA_SLOT13_ALT:
  921. - case R_XTENSA_SLOT14_ALT:
  922. - case R_XTENSA_ASM_EXPAND:
  923. - case R_XTENSA_ASM_SIMPLIFY:
  924. - case R_XTENSA_DIFF8:
  925. - case R_XTENSA_DIFF16:
  926. - case R_XTENSA_DIFF32:
  927. -#if HAVE_BFD_XTENSA_PDIFF_RELOCS
  928. - case R_XTENSA_PDIFF8:
  929. - case R_XTENSA_PDIFF16:
  930. - case R_XTENSA_PDIFF32:
  931. - case R_XTENSA_NDIFF8:
  932. - case R_XTENSA_NDIFF16:
  933. - case R_XTENSA_NDIFF32:
  934. -#endif
  935. - case R_XTENSA_32_PCREL:
  936. - continue;
  937. - case R_XTENSA_32:
  938. - case R_XTENSA_PLT:
  939. - if (bfd_big_endian (abs_bfd))
  940. - sym_addr =
  941. - (r_mem[0] << 24)
  942. - + (r_mem[1] << 16)
  943. - + (r_mem[2] << 8)
  944. - + r_mem[3];
  945. - else
  946. - sym_addr =
  947. - r_mem[0]
  948. - + (r_mem[1] << 8)
  949. - + (r_mem[2] << 16)
  950. - + (r_mem[3] << 24);
  951. - relocation_needed = 1;
  952. - break;
  953. - default:
  954. - goto bad_resolved_reloc;
  955. -#elif defined(TARGET_riscv64)
  956. - case R_RISCV_32_PCREL:
  957. - case R_RISCV_ADD32:
  958. - case R_RISCV_ADD64:
  959. - case R_RISCV_SUB32:
  960. - case R_RISCV_SUB64:
  961. - continue;
  962. - case R_RISCV_32:
  963. - case R_RISCV_64:
  964. - goto good_32bit_resolved_reloc;
  965. - default:
  966. - goto bad_resolved_reloc;
  967. -#else
  968. - default:
  969. - /* The default is to assume that the
  970. - relocation is relative and has
  971. - already been fixed up by the
  972. - linker (perhaps we ought to make
  973. - give an error by default, and
  974. - require `safe' relocations to be
  975. - enumberated explicitly?). */
  976. - goto good_32bit_resolved_reloc;
  977. -#endif
  978. - good_32bit_resolved_reloc:
  979. - if (bfd_big_endian (abs_bfd))
  980. - sym_addr =
  981. - (r_mem[0] << 24)
  982. - + (r_mem[1] << 16)
  983. - + (r_mem[2] << 8)
  984. - + r_mem[3];
  985. - else
  986. - sym_addr =
  987. - r_mem[0]
  988. - + (r_mem[1] << 8)
  989. - + (r_mem[2] << 16)
  990. - + (r_mem[3] << 24);
  991. - relocation_needed = 1;
  992. - update_text = 0;
  993. - break;
  994. -
  995. - bad_resolved_reloc:
  996. - printf("ERROR: reloc type %s unsupported in this context\n",
  997. - q->howto->name);
  998. - bad_relocs++;
  999. - break;
  1000. - }
  1001. - } else {
  1002. - /* Calculate the sym address ourselves. */
  1003. -#if defined(TARGET_xtensa)
  1004. - /* For xtensa, calculation of addresses won't
  1005. - work this way. binutils "ld -r" generate
  1006. - different relocation types, among others
  1007. - type 20, R_XTENSA_SLOT0_OP. The latter is
  1008. - produced for various opcodes that differ
  1009. - in size and format, some will have the
  1010. - addend filled in when linking, others won't.
  1011. - For elf2flt to handle these relocations
  1012. - would involve analysing the opcodes in
  1013. - detail. Therefore, elf2flt for xtensa is
  1014. - patched to work with "-a" option solely,
  1015. - which will take output of "ld -q".
  1016. - */
  1017. - fatal("ERROR: cannot run without '-a'");
  1018. -#endif
  1019. - sym_reloc_size = bfd_get_reloc_size(q->howto);
  1020. -
  1021. - if (sym_reloc_size == 0) {
  1022. - /* These are dummy relocs that can be ignored */
  1023. - continue;
  1024. - }
  1025. -
  1026. -#if !defined(TARGET_h8300) && !defined(TARGET_e1) && !defined(TARGET_bfin) && !defined(TARGET_m68k)
  1027. - if (sym_reloc_size != 4) {
  1028. - printf("ERROR: bad reloc type %d size=%d for symbol=%s\n",
  1029. - (*p)->howto->type, sym_reloc_size, sym_name);
  1030. - bad_relocs++;
  1031. - rc = -1;
  1032. - continue;
  1033. - }
  1034. -#endif
  1035. -
  1036. - switch ((*p)->howto->type) {
  1037. -
  1038. -#if defined(TARGET_m68k)
  1039. - case R_68K_32:
  1040. - relocation_needed = 1;
  1041. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1042. - sym_addr += sym_vma + q->addend;
  1043. - break;
  1044. - case R_68K_PC16:
  1045. - case R_68K_PC32:
  1046. - sym_vma = 0;
  1047. - sym_addr += sym_vma + q->addend;
  1048. - sym_addr -= q->address;
  1049. - break;
  1050. -#endif
  1051. -
  1052. -#if defined(TARGET_arm)
  1053. - case R_ARM_ABS32:
  1054. - relocation_needed = 1;
  1055. - if (verbose)
  1056. - fprintf(stderr,
  1057. - "%s vma=0x%x, "
  1058. - "value=0x%"BFD_VMA_FMT"x, "
  1059. - "address=0x%"BFD_VMA_FMT"x "
  1060. - "sym_addr=0x%x rs=0x%x, opcode=0x%x\n",
  1061. - "ABS32",
  1062. - sym_vma, (*(q->sym_ptr_ptr))->value,
  1063. - q->address, sym_addr,
  1064. - (*p)->howto->rightshift,
  1065. - *(uint32_t *)r_mem);
  1066. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1067. - sym_addr += sym_vma + q->addend;
  1068. - break;
  1069. - case R_ARM_GOT32:
  1070. - case R_ARM_GOTPC:
  1071. - /* Should be fine as is */
  1072. - break;
  1073. - case R_ARM_PLT32:
  1074. - if (verbose)
  1075. - fprintf(stderr,
  1076. - "%s vma=0x%x, "
  1077. - "value=0x%"BFD_VMA_FMT"x, "
  1078. - "address=0x%"BFD_VMA_FMT"x "
  1079. - "sym_addr=0x%x rs=0x%x, opcode=0x%x\n",
  1080. - "PLT32",
  1081. - sym_vma, (*(q->sym_ptr_ptr))->value,
  1082. - q->address, sym_addr,
  1083. - (*p)->howto->rightshift,
  1084. - *(uint32_t *)r_mem);
  1085. - case R_ARM_PC24:
  1086. - sym_vma = 0;
  1087. - sym_addr = (sym_addr-q->address)>>(*p)->howto->rightshift;
  1088. - break;
  1089. -#endif
  1090. -
  1091. -#ifdef TARGET_v850
  1092. - case R_V850_ABS32:
  1093. - relocation_needed = 1;
  1094. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1095. - sym_addr += sym_vma + q->addend;
  1096. - break;
  1097. - case R_V850_ZDA_16_16_OFFSET:
  1098. - case R_V850_ZDA_16_16_SPLIT_OFFSET:
  1099. - /* Can't support zero-relocations. */
  1100. - printf ("ERROR: %s+0x%"BFD_VMA_FMT"x: zero relocations not supported\n",
  1101. - sym_name, q->addend);
  1102. - continue;
  1103. -#endif /* TARGET_v850 */
  1104. -
  1105. -#ifdef TARGET_h8300
  1106. - case R_H8_DIR24R8:
  1107. - if (sym_reloc_size != 4) {
  1108. - printf("R_H8_DIR24R8 size %d\n", sym_reloc_size);
  1109. - bad_relocs++;
  1110. - continue;
  1111. - }
  1112. - relocation_needed = 1;
  1113. - sym_addr = (*(q->sym_ptr_ptr))->value;
  1114. - q->address -= 1;
  1115. - r_mem -= 1; /* tracks q->address */
  1116. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1117. - sym_addr += sym_vma + q->addend;
  1118. - sym_addr |= (*(unsigned char *)r_mem<<24);
  1119. - break;
  1120. - case R_H8_DIR24A8:
  1121. - if (sym_reloc_size != 4) {
  1122. - printf("R_H8_DIR24A8 size %d\n", sym_reloc_size);
  1123. - bad_relocs++;
  1124. - continue;
  1125. - }
  1126. - /* Absolute symbol done not relocation */
  1127. - relocation_needed = !bfd_is_abs_section(sym_section);
  1128. - sym_addr = (*(q->sym_ptr_ptr))->value;
  1129. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1130. - sym_addr += sym_vma + q->addend;
  1131. - break;
  1132. - case R_H8_DIR32:
  1133. - case R_H8_DIR32A16: /* currently 32, could be made 16 */
  1134. - case R_H8_DISP32A16:
  1135. - if (sym_reloc_size != 4) {
  1136. - printf("R_H8_DIR32 size %d\n", sym_reloc_size);
  1137. - bad_relocs++;
  1138. - continue;
  1139. - }
  1140. - relocation_needed = 1;
  1141. - sym_addr = (*(q->sym_ptr_ptr))->value;
  1142. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1143. - sym_addr += sym_vma + q->addend;
  1144. - break;
  1145. - case R_H8_PCREL16:
  1146. - relocation_needed = 0;
  1147. - sym_vma = 0;
  1148. - sym_addr = (*(q->sym_ptr_ptr))->value;
  1149. - sym_addr += sym_vma + q->addend;
  1150. - sym_addr -= (q->address + 2);
  1151. - if (bfd_big_endian(abs_bfd))
  1152. - *(unsigned short *)r_mem =
  1153. - bfd_big_endian(abs_bfd) ? htons(sym_addr) : sym_addr;
  1154. - continue;
  1155. - case R_H8_PCREL8:
  1156. - relocation_needed = 0;
  1157. - sym_vma = 0;
  1158. - sym_addr = (*(q->sym_ptr_ptr))->value;
  1159. - sym_addr += sym_vma + q->addend;
  1160. - sym_addr -= (q->address + 1);
  1161. - *(unsigned char *)r_mem = sym_addr;
  1162. - continue;
  1163. -#endif
  1164. -
  1165. -#ifdef TARGET_microblaze
  1166. - case R_MICROBLAZE_64:
  1167. - /* work out the relocation */
  1168. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1169. - sym_addr += sym_vma + q->addend;
  1170. - /* Write relocated pointer back */
  1171. - r_mem[2] = (sym_addr >> 24) & 0xff;
  1172. - r_mem[3] = (sym_addr >> 16) & 0xff;
  1173. - r_mem[6] = (sym_addr >> 8) & 0xff;
  1174. - r_mem[7] = sym_addr & 0xff;
  1175. - relocation_needed = 1;
  1176. - /* The symbol is split over two consecutive instructions.
  1177. - Flag this to the flat loader by setting the high bit of
  1178. - the relocation symbol. */
  1179. - pflags = 0x80000000;
  1180. - break;
  1181. - case R_MICROBLAZE_32:
  1182. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1183. - sym_addr += sym_vma + q->addend;
  1184. - relocation_needed = 1;
  1185. - break;
  1186. - case R_MICROBLAZE_64_PCREL:
  1187. - sym_vma = 0;
  1188. - sym_addr += sym_vma + q->addend;
  1189. - sym_addr -= (q->address + 4);
  1190. - sym_addr = htonl(sym_addr);
  1191. - /* insert 16 MSB */
  1192. - * ((unsigned short *) (r_mem+2)) = (sym_addr) & 0xFFFF;
  1193. - /* then 16 LSB */
  1194. - * ((unsigned short *) (r_mem+6)) = (sym_addr >> 16) & 0xFFFF;
  1195. - /* We've done all the work, so continue
  1196. - to next reloc instead of break */
  1197. - continue;
  1198. - /* These are already relocated for us as text-relative, or are dummy entries */
  1199. - case R_MICROBLAZE_PLT_64:
  1200. - case R_MICROBLAZE_NONE:
  1201. - case R_MICROBLAZE_64_NONE:
  1202. - case R_MICROBLAZE_32_PCREL_LO:
  1203. - relocation_needed = 0;
  1204. - continue;
  1205. -
  1206. -#endif /* TARGET_microblaze */
  1207. -
  1208. -#ifdef TARGET_nios2
  1209. -#define htoniosl(x) (x)
  1210. -#define niostohl(x) (x)
  1211. - case R_NIOS2_BFD_RELOC_32:
  1212. - relocation_needed = 1;
  1213. - pflags = (FLAT_NIOS2_R_32 << 28);
  1214. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1215. - sym_addr += sym_vma + q->addend;
  1216. - /* modify target, in target order */
  1217. - *(unsigned long *)r_mem = htoniosl(sym_addr);
  1218. - break;
  1219. - case R_NIOS2_CALL26:
  1220. - {
  1221. - unsigned long exist_val;
  1222. - relocation_needed = 1;
  1223. - pflags = (FLAT_NIOS2_R_CALL26 << 28);
  1224. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1225. - sym_addr += sym_vma + q->addend;
  1226. -
  1227. - /* modify target, in target order */
  1228. - // exist_val = niostohl(*(unsigned long *)r_mem);
  1229. - exist_val = ((sym_addr >> 2) << 6);
  1230. - *(unsigned long *)r_mem = htoniosl(exist_val);
  1231. - break;
  1232. - }
  1233. - case R_NIOS2_HIADJ16:
  1234. - case R_NIOS2_HI16:
  1235. - {
  1236. - unsigned long exist_val;
  1237. - int r2_type;
  1238. - /* handle the adjacent HI/LO pairs */
  1239. - if (relcount == 0)
  1240. - r2_type = R_NIOS2_NONE;
  1241. - else
  1242. - r2_type = p[1]->howto->type;
  1243. - if ((r2_type == R_NIOS2_LO16)
  1244. - && (p[0]->sym_ptr_ptr == p[1]->sym_ptr_ptr)
  1245. - && (p[0]->addend == p[1]->addend))
  1246. - {
  1247. - unsigned char * r2_mem = sectionp + p[1]->address;
  1248. - if (p[1]->address - q->address!=4)
  1249. - printf("Err: HI/LO not adjacent %ld\n", p[1]->address - q->address);
  1250. - relocation_needed = 1;
  1251. - pflags = (q->howto->type == R_NIOS2_HIADJ16)
  1252. - ? FLAT_NIOS2_R_HIADJ_LO : FLAT_NIOS2_R_HI_LO;
  1253. - pflags <<= 28;
  1254. -
  1255. - sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  1256. - sym_addr += sym_vma + q->addend;
  1257. -
  1258. - /* modify high 16 bits, in target order */
  1259. - exist_val = niostohl(*(unsigned long *)r_mem);
  1260. - exist_val = ((exist_val >> 22) << 22) | (exist_val & 0x3f);
  1261. - if (q->howto->type == R_NIOS2_HIADJ16)
  1262. - exist_val |= ((((sym_addr >> 16) + ((sym_addr >> 15) & 1)) & 0xFFFF) << 6);
  1263. - else
  1264. - exist_val |= (((sym_addr >> 16) & 0xFFFF) << 6);
  1265. - *(unsigned long *)r_mem = htoniosl(exist_val);
  1266. -
  1267. - /* modify low 16 bits, in target order */
  1268. - exist_val = niostohl(*(unsigned long *)r2_mem);
  1269. - exist_val = ((exist_val >> 22) << 22) | (exist_val & 0x3f);
  1270. - exist_val |= ((sym_addr & 0xFFFF) << 6);
  1271. - *(unsigned long *)r2_mem = htoniosl(exist_val);
  1272. -
  1273. - } else
  1274. - goto NIOS2_RELOC_ERR;
  1275. - }
  1276. - break;
  1277. -
  1278. - case R_NIOS2_GPREL:
  1279. - {
  1280. - unsigned long exist_val, temp;
  1281. - //long gp = get_symbol_offset("_gp", sym_section, symbols, number_of_symbols);
  1282. - long gp = get_gp_value(symbols, number_of_symbols);
  1283. - if (gp == -1) {
  1284. - printf("Err: unresolved symbol _gp when relocating %s\n", sym_name);
  1285. - goto NIOS2_RELOC_ERR;
  1286. - }
  1287. - /* _gp holds a absolute value, otherwise the ld cannot generate correct code */
  1288. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1289. - //printf("sym=%x, %d, _gp=%x, %d\n", sym_addr+sym_vma, sym_addr+sym_vma, gp, gp);
  1290. - sym_addr += sym_vma + q->addend;
  1291. - sym_addr -= gp;
  1292. - //printf("sym - _gp=%x, %d\n", sym_addr, sym_addr);
  1293. - /* modify the target, in target order (little_endian) */
  1294. - exist_val = niostohl(*(unsigned long *)r_mem);
  1295. - temp = ((exist_val >> 6) & 0x3ff0000) | (sym_addr & 0xffff);
  1296. - temp <<= 6;
  1297. - temp |= (exist_val & 0x3f);
  1298. - *(unsigned long *)r_mem = htoniosl(temp);
  1299. - if (verbose)
  1300. - printf("omit: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  1301. - "section=%s size=%d "
  1302. - "fixup=0x%x (reloc=0x%"BFD_VMA_FMT"x) GPREL\n",
  1303. - q->address, sym_name, addstr,
  1304. - section_name, sym_reloc_size,
  1305. - sym_addr, section_vma + q->address);
  1306. - continue;
  1307. - }
  1308. - case R_NIOS2_PCREL16:
  1309. - {
  1310. - unsigned long exist_val;
  1311. - sym_vma = 0;
  1312. - sym_addr += sym_vma + q->addend;
  1313. - sym_addr -= (q->address + 4);
  1314. - /* modify the target, in target order (little_endian) */
  1315. - exist_val = niostohl(*(unsigned long *)r_mem);
  1316. - exist_val = ((exist_val >> 22) << 22) | (exist_val & 0x3f);
  1317. - exist_val |= ((sym_addr & 0xFFFF) << 6);
  1318. - *(unsigned long *)r_mem = htoniosl(exist_val);
  1319. - if (verbose)
  1320. - printf("omit: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  1321. - "section=%s size=%d "
  1322. - "fixup=0x%x (reloc=0x%"BFD_VMA_FMT"x) PCREL\n",
  1323. - q->address, sym_name, addstr,
  1324. - section_name, sym_reloc_size,
  1325. - sym_addr, section_vma + q->address);
  1326. - continue;
  1327. - }
  1328. -
  1329. - case R_NIOS2_LO16:
  1330. - /* check if this is actually the 2nd half of a pair */
  1331. - if ((p > relpp)
  1332. - && ((p[-1]->howto->type == R_NIOS2_HIADJ16)
  1333. - || (p[-1]->howto->type == R_NIOS2_HI16))
  1334. - && (p[-1]->sym_ptr_ptr == p[0]->sym_ptr_ptr)
  1335. - && (p[-1]->addend == p[0]->addend)) {
  1336. - if (verbose)
  1337. - printf("omit: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  1338. - "section=%s size=%d LO16\n",
  1339. - q->address, sym_name, addstr,
  1340. - section_name, sym_reloc_size);
  1341. - continue;
  1342. - }
  1343. -
  1344. - /* error, fall through */
  1345. -
  1346. - case R_NIOS2_S16:
  1347. - case R_NIOS2_U16:
  1348. - case R_NIOS2_CACHE_OPX:
  1349. - case R_NIOS2_IMM5:
  1350. - case R_NIOS2_IMM6:
  1351. - case R_NIOS2_IMM8:
  1352. - case R_NIOS2_BFD_RELOC_16:
  1353. - case R_NIOS2_BFD_RELOC_8:
  1354. - case R_NIOS2_GNU_VTINHERIT:
  1355. - case R_NIOS2_GNU_VTENTRY:
  1356. - case R_NIOS2_UJMP:
  1357. - case R_NIOS2_CJMP:
  1358. - case R_NIOS2_CALLR:
  1359. -NIOS2_RELOC_ERR:
  1360. - printf("Err: unexpected reloc type %s(%d)\n", q->howto->name, q->howto->type);
  1361. - bad_relocs++;
  1362. - continue;
  1363. -#endif /* TARGET_nios2 */
  1364. -
  1365. -#ifdef TARGET_sparc
  1366. - case R_SPARC_32:
  1367. - case R_SPARC_UA32:
  1368. - relocation_needed = 1;
  1369. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1370. - sym_addr += sym_vma + q->addend;
  1371. - break;
  1372. - case R_SPARC_PC22:
  1373. - sym_vma = 0;
  1374. - sym_addr += sym_vma + q->addend;
  1375. - sym_addr -= q->address;
  1376. - break;
  1377. - case R_SPARC_WDISP30:
  1378. - sym_addr = (((*(q->sym_ptr_ptr))->value-
  1379. - q->address) >> 2) & 0x3fffffff;
  1380. - sym_addr |= (
  1381. - ntohl(*(uint32_t *)r_mem)
  1382. - & 0xc0000000
  1383. - );
  1384. - break;
  1385. - case R_SPARC_HI22:
  1386. - relocation_needed = 1;
  1387. - pflags = 0x80000000;
  1388. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1389. - sym_addr += sym_vma + q->addend;
  1390. - sym_addr |= (
  1391. - htonl(*(uint32_t *)r_mem)
  1392. - & 0xffc00000
  1393. - );
  1394. - break;
  1395. - case R_SPARC_LO10:
  1396. - relocation_needed = 1;
  1397. - pflags = 0x40000000;
  1398. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1399. - sym_addr += sym_vma + q->addend;
  1400. - sym_addr &= 0x000003ff;
  1401. - sym_addr |= (
  1402. - htonl(*(uint32_t *)r_mem)
  1403. - & 0xfffffc00
  1404. - );
  1405. - break;
  1406. -#endif /* TARGET_sparc */
  1407. -
  1408. -
  1409. -#ifdef TARGET_sh
  1410. - case R_SH_DIR32:
  1411. - relocation_needed = 1;
  1412. - sym_vma = elf2flt_bfd_section_vma(sym_section);
  1413. - sym_addr += sym_vma + q->addend;
  1414. - break;
  1415. - case R_SH_REL32:
  1416. - sym_vma = 0;
  1417. - sym_addr += sym_vma + q->addend;
  1418. - sym_addr -= q->address;
  1419. - break;
  1420. -#endif /* TARGET_sh */
  1421. -
  1422. -#ifdef TARGET_e1
  1423. -#define htoe1l(x) htonl(x)
  1424. -
  1425. -#if 0
  1426. -#define DEBUG_E1
  1427. -#endif
  1428. -
  1429. -#ifdef DEBUG_E1
  1430. -#define DBG_E1 printf
  1431. -#else
  1432. -#define DBG_E1(x, ... )
  1433. -#endif
  1434. -
  1435. -#define _32BITS_RELOC 0x00000000
  1436. -#define _30BITS_RELOC 0x80000000
  1437. -#define _28BITS_RELOC 0x40000000
  1438. - {
  1439. - char *p;
  1440. - unsigned long sec_vma, exist_val, S;
  1441. - case R_E1_CONST31:
  1442. - relocation_needed = 1;
  1443. - DBG_E1("Handling Reloc <CONST31>\n");
  1444. - sec_vma = elf2flt_bfd_section_vma(sym_section);
  1445. - DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  1446. - sec_vma, sym_addr, q->address);
  1447. - sym_addr = sec_vma + sym_addr;
  1448. - exist_val = *(unsigned long*)((unsigned long)sectionp + q->address + 2);
  1449. - DBG_E1("Orig:exist_val : [0x%08x]\n", exist_val);
  1450. - exist_val = htoe1l(exist_val);
  1451. - DBG_E1("HtoBE:exist_val : [0x%08x]\n", exist_val);
  1452. - sym_addr += exist_val;
  1453. - pflags = _30BITS_RELOC;
  1454. - break;
  1455. - case R_E1_CONST31_PCREL:
  1456. - relocation_needed = 0;
  1457. - DBG_E1("Handling Reloc <CONST31_PCREL>\n");
  1458. - DBG_E1("DONT RELOCATE AT LOADING\n");
  1459. - sec_vma = elf2flt_bfd_section_vma(sym_section);
  1460. - DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  1461. - sec_vma, sym_addr, q->address);
  1462. - sym_addr = sec_vma + sym_addr;
  1463. - DBG_E1("sym_addr = sec_vma + sym_addr : [0x%x]\n", sym_addr );
  1464. -
  1465. - DBG_E1("q->address : 0x%x, section_vma : 0x%x\n", q->address,
  1466. - section_vma );
  1467. - q->address = q->address + section_vma;
  1468. - DBG_E1("q->address += section_vma : 0x%x\n", q->address );
  1469. -
  1470. - if( (sym_addr = (sym_addr - q->address - 6)) < 0 )
  1471. - DBG_E1("NEGATIVE OFFSET in PC Relative instruction\n");
  1472. - DBG_E1( "sym_addr := sym_addr - q->address - "
  1473. - "sizeof(CONST31_PCREL): [0x%x]\n",
  1474. - sym_addr );
  1475. - exist_val = *(unsigned long*)((unsigned long)sectionp + q->address + 2);
  1476. - DBG_E1("Orig:exist_val : [0x%08x]\n", exist_val);
  1477. - exist_val = htoe1l(exist_val);
  1478. - DBG_E1("HtoBE:exist_val : [0x%08x]\n", exist_val);
  1479. - sym_addr |= exist_val;
  1480. - DBG_E1("sym_addr |= exist_val) : [0x%x]\n", sym_addr );
  1481. - break;
  1482. - case R_E1_DIS29W_PCREL:
  1483. - relocation_needed = 0;
  1484. - DBG_E1("Handling Reloc <DIS29W_PCREL>\n");
  1485. - DBG_E1("DONT RELOCATE AT LOADING\n");
  1486. - sec_vma = elf2flt_bfd_section_vma(sym_section);
  1487. - DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  1488. - sec_vma, sym_addr, q->address);
  1489. - sym_addr = sec_vma + sym_addr;
  1490. - DBG_E1("sym_addr = sec_vma + sym_addr : [0x%x]\n", sym_addr );
  1491. -
  1492. - DBG_E1("q->address : 0x%x, section_vma : 0x%x\n", q->address,
  1493. - section_vma );
  1494. - q->address = q->address + section_vma;
  1495. - DBG_E1("q->address += section_vma : 0x%x\n", q->address );
  1496. -
  1497. - if( (sym_addr = (sym_addr - q->address - 6)) < 0 )
  1498. - DBG_E1("NEGATIVE OFFSET in PC Relative instruction\n");
  1499. - DBG_E1( "sym_addr := sym_addr - q->address - "
  1500. - "sizeof(CONST31_PCREL): [0x%x]\n",
  1501. - sym_addr );
  1502. - DBG_E1("sectionp:[0x%x], q->address:[0x%x]\n", sectionp, q->address );
  1503. - exist_val = *(unsigned long*)((unsigned long)sectionp + q->address + 2);
  1504. - DBG_E1("Original:exist_val : [0x%08x]\n",exist_val);
  1505. - exist_val = htoe1l(exist_val);
  1506. - DBG_E1("HtoBE:exist_val : [0x%08x]\n",exist_val);
  1507. - sym_addr += exist_val;
  1508. - break;
  1509. - case R_E1_DIS29W:
  1510. - DBG_E1("Handling Reloc <DIS29W>\n");
  1511. - goto DIS29_RELOCATION;
  1512. - case R_E1_DIS29H:
  1513. - DBG_E1("Handling Reloc <DIS29H>\n");
  1514. - goto DIS29_RELOCATION;
  1515. - case R_E1_DIS29B:
  1516. - DBG_E1("Handling Reloc <DIS29B>\n");
  1517. -DIS29_RELOCATION:
  1518. - relocation_needed = 1;
  1519. - sec_vma = elf2flt_bfd_section_vma(sym_section);
  1520. - DBG_E1("sec_vma : [0x%x], sym_addr : [0x%08x]\n",
  1521. - sec_vma, sym_addr);
  1522. - sym_addr = sec_vma + sym_addr;
  1523. - DBG_E1("sym_addr = sec_vma + sym_addr : [0x%08x]\n", sym_addr);
  1524. - exist_val = *(unsigned long*)((unsigned long)sectionp + q->address + 2);
  1525. - DBG_E1("Orig:exist_val : [0x%08x]\n", exist_val);
  1526. - exist_val = htoe1l(exist_val);
  1527. - DBG_E1("HtoBE:exist_val : [0x%08x]\n", exist_val);
  1528. - sym_addr += exist_val;
  1529. - DBG_E1("sym_addr += exist_val : [0x%08x]\n", sym_addr);
  1530. - pflags = _28BITS_RELOC;
  1531. - break;
  1532. - case R_E1_IMM32_PCREL:
  1533. - relocation_needed = 0;
  1534. - DBG_E1("Handling Reloc <IMM32_PCREL>\n");
  1535. - DBG_E1("DONT RELOCATE AT LOADING\n");
  1536. - sec_vma = elf2flt_bfd_section_vma(sym_section);
  1537. - DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  1538. - sec_vma, sym_addr);
  1539. - sym_addr = sec_vma + sym_addr;
  1540. -
  1541. - DBG_E1("sym_addr = sec_vma + sym_addr : [0x%x]\n", sym_addr );
  1542. - DBG_E1("q->address : 0x%x, section_vma : 0x%x\n", q->address,
  1543. - section_vma );
  1544. - q->address = q->address + section_vma;
  1545. - DBG_E1("q->address += section_vma : 0x%x\n", q->address );
  1546. -
  1547. - if( (sym_addr = (sym_addr - q->address - 6 )) < 0 )
  1548. - DBG_E1("NEGATIVE OFFSET in PC Relative instruction\n");
  1549. - DBG_E1( "sym_addr := sym_addr - q->address - "
  1550. - "sizeof(CONST31_PCREL): [0x%x]\n",
  1551. - sym_addr );
  1552. - DBG_E1("sectionp:[0x%x], q->address:[0x%x]\n", sectionp, q->address );
  1553. - exist_val = *(unsigned long*)((unsigned long)sectionp + q->address + 2);
  1554. - DBG_E1("Original:exist_val : [0x%08x]\n",exist_val);
  1555. - exist_val = htoe1l(exist_val);
  1556. - DBG_E1("HtoBE:exist_val : [0x%08x]\n",exist_val);
  1557. - sym_addr += exist_val;
  1558. - break;
  1559. - case R_E1_IMM32:
  1560. - relocation_needed = 1;
  1561. - DBG_E1("Handling Reloc <IMM32>\n");
  1562. - sec_vma = elf2flt_bfd_section_vma(sym_section);
  1563. - DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  1564. - sec_vma, sym_addr);
  1565. - sym_addr = sec_vma + sym_addr;
  1566. - DBG_E1("sym_addr = sec_vma + sym_addr : [0x%x]\n", sym_addr );
  1567. - DBG_E1("sectionp:[0x%x], q->address:[0x%x]\n", sectionp, q->address );
  1568. - exist_val = *(unsigned long*)((unsigned long)sectionp + q->address + 2);
  1569. - DBG_E1("Original:exist_val : [0x%08x]\n",exist_val);
  1570. - exist_val = htoe1l(exist_val);
  1571. - DBG_E1("HtoBE:exist_val : [0x%08x]\n",exist_val);
  1572. - sym_addr += exist_val;
  1573. - pflags = _32BITS_RELOC;
  1574. - break;
  1575. - case R_E1_WORD:
  1576. - relocation_needed = 1;
  1577. - DBG_E1("Handling Reloc <WORD>\n");
  1578. - sec_vma = elf2flt_bfd_section_vma(sym_section);
  1579. - DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  1580. - sec_vma, sym_addr);
  1581. - sym_addr = sec_vma + sym_addr;
  1582. - DBG_E1("sym_addr = sec_vma + sym_addr : [0x%x]\n", sym_addr );
  1583. - exist_val = *(unsigned long*)((unsigned long)sectionp + q->address );
  1584. - DBG_E1("Orig:exist_val : [0x%08x]\n", exist_val);
  1585. - exist_val = htoe1l(exist_val);
  1586. - DBG_E1("HtoBE:exist_val : [0x%08x]\n", exist_val);
  1587. - sym_addr += exist_val;
  1588. - DBG_E1("sym_addr += exist_val : [0x%08x]\n", sym_addr);
  1589. - pflags = _32BITS_RELOC;
  1590. - break;
  1591. - }
  1592. -#undef _32BITS_RELOC
  1593. -#undef _30BITS_RELOC
  1594. -#undef _28BITS_RELOC
  1595. -#endif
  1596. - default:
  1597. - /* missing support for other types of relocs */
  1598. - printf("ERROR: bad reloc type (%s)%d\n", q->howto->name, (*p)->howto->type);
  1599. - bad_relocs++;
  1600. - continue;
  1601. - }
  1602. - }
  1603. -
  1604. - sprintf(&addstr[0], "+0x%lx", sym_addr - (*(q->sym_ptr_ptr))->value -
  1605. - elf2flt_bfd_section_vma(sym_section));
  1606. -
  1607. -
  1608. - /*
  1609. - * for full elf relocation we have to write back the
  1610. - * start_code relative value to use. Not needed with pic_with_got
  1611. - * or if the fixup has already been done above (in which case update_text was set to 0)
  1612. - */
  1613. - if (update_text) {
  1614. -#if defined(TARGET_arm)
  1615. - union {
  1616. - unsigned char c[4];
  1617. - uint32_t l;
  1618. - } tmp;
  1619. - int32_t hl;
  1620. - int i0, i1, i2, i3;
  1621. -
  1622. - /*
  1623. - * horrible nasty hack to support different endianess
  1624. - */
  1625. - if (!bfd_big_endian(abs_bfd)) {
  1626. - i0 = 0;
  1627. - i1 = 1;
  1628. - i2 = 2;
  1629. - i3 = 3;
  1630. - } else {
  1631. - i0 = 3;
  1632. - i1 = 2;
  1633. - i2 = 1;
  1634. - i3 = 0;
  1635. - }
  1636. -
  1637. - tmp.l = *(uint32_t *)r_mem;
  1638. - hl = tmp.c[i0] | (tmp.c[i1] << 8) | (tmp.c[i2] << 16);
  1639. - if (use_resolved ||
  1640. - (((*p)->howto->type != R_ARM_PC24) &&
  1641. - ((*p)->howto->type != R_ARM_PLT32)))
  1642. - hl |= (tmp.c[i3] << 24);
  1643. - else if (tmp.c[i2] & 0x80)
  1644. - hl |= 0xff000000; /* sign extend */
  1645. - if (!use_resolved)
  1646. - hl += sym_addr;
  1647. - tmp.c[i0] = hl & 0xff;
  1648. - tmp.c[i1] = (hl >> 8) & 0xff;
  1649. - tmp.c[i2] = (hl >> 16) & 0xff;
  1650. - if (use_resolved ||
  1651. - (((*p)->howto->type != R_ARM_PC24) &&
  1652. - ((*p)->howto->type != R_ARM_PLT32)))
  1653. - tmp.c[i3] = (hl >> 24) & 0xff;
  1654. - if (((*p)->howto->type == R_ARM_ABS32) ||
  1655. - ((*p)->howto->type == R_ARM_TARGET1) ||
  1656. - ((*p)->howto->type == R_ARM_TARGET2))
  1657. - *(uint32_t *)r_mem = htonl(hl);
  1658. - else
  1659. - *(uint32_t *)r_mem = tmp.l;
  1660. -#elif defined(TARGET_e1)
  1661. -#define OPCODE_SIZE 2 /* Add 2 bytes, counting the opcode size*/
  1662. - switch ((*p)->howto->type) {
  1663. - case R_E1_CONST31:
  1664. - case R_E1_CONST31_PCREL:
  1665. - case R_E1_DIS29W_PCREL:
  1666. - case R_E1_DIS29W:
  1667. - case R_E1_DIS29H:
  1668. - case R_E1_DIS29B:
  1669. - case R_E1_IMM32_PCREL:
  1670. - case R_E1_IMM32:
  1671. - DBG_E1("In addr + 2:[0x%x] <- write [0x%x]\n",
  1672. - (sectionp + q->address + 2), sym_addr );
  1673. - *((unsigned long *) (sectionp + q->address + OPCODE_SIZE)) =
  1674. - htonl(sym_addr);
  1675. - break;
  1676. - case R_E1_WORD:
  1677. - DBG_E1("In addr : [0x%x] <- write [0x%x]\n",
  1678. - (sectionp + q->address), sym_addr );
  1679. - *((unsigned long *) (sectionp + q->address )) = htonl(sym_addr);
  1680. - break;
  1681. - default:
  1682. - fatal("ERROR: Unhandled Relocation: 0x%x", (*p)->howto->type);
  1683. - }
  1684. -#elif defined TARGET_bfin
  1685. - if ((*p)->howto->type == R_BFIN_RIMM16
  1686. - || (*p)->howto->type == R_BFIN_HUIMM16
  1687. - || (*p)->howto->type == R_BFIN_LUIMM16)
  1688. - {
  1689. - /* for l and h we set the lower 16 bits which is only when it will be used */
  1690. - bfd_putl16 (sym_addr, sectionp + q->address);
  1691. - } else if ((*p)->howto->type == R_BFIN_BYTE4_DATA) {
  1692. - bfd_putl32 (sym_addr, sectionp + q->address);
  1693. - }
  1694. -#else /* ! TARGET_arm && ! TARGET_e1 && ! TARGET_bfin */
  1695. -
  1696. - switch (q->howto->type) {
  1697. -#ifdef TARGET_v850
  1698. - case R_V850_HI16_S:
  1699. - case R_V850_HI16:
  1700. - case R_V850_LO16:
  1701. - /* Do nothing -- for cases we handle,
  1702. - the bits produced by the linker are
  1703. - what we want in the final flat file
  1704. - (and other cases are errors). Note
  1705. - that unlike most relocated values,
  1706. - it is stored in little-endian order,
  1707. - but this is necessary to avoid
  1708. - trashing the low-bit, and the float
  1709. - loaders knows about it. */
  1710. - break;
  1711. -#endif /* TARGET_V850 */
  1712. -
  1713. -#ifdef TARGET_nios2
  1714. - case R_NIOS2_BFD_RELOC_32:
  1715. - case R_NIOS2_CALL26:
  1716. - case R_NIOS2_HIADJ16:
  1717. - case R_NIOS2_HI16:
  1718. - /* do nothing */
  1719. - break;
  1720. -#endif /* TARGET_nios2 */
  1721. -
  1722. -#if defined(TARGET_m68k)
  1723. - case R_68K_PC16:
  1724. - if (sym_addr < -0x8000 || sym_addr > 0x7fff) {
  1725. - fprintf (stderr, "Relocation overflow for R_68K_PC16 relocation against %s\n", sym_name);
  1726. - bad_relocs++;
  1727. - } else {
  1728. - r_mem[0] = (sym_addr >> 8) & 0xff;
  1729. - r_mem[1] = sym_addr & 0xff;
  1730. - }
  1731. - break;
  1732. -#endif
  1733. -
  1734. - default:
  1735. - /* The alignment of the build host
  1736. - might be stricter than that of the
  1737. - target, so be careful. We store in
  1738. - network byte order. */
  1739. - r_mem[0] = (sym_addr >> 24) & 0xff;
  1740. - r_mem[1] = (sym_addr >> 16) & 0xff;
  1741. - r_mem[2] = (sym_addr >> 8) & 0xff;
  1742. - r_mem[3] = sym_addr & 0xff;
  1743. - }
  1744. -#endif /* !TARGET_arm */
  1745. - }
  1746. -
  1747. - /*
  1748. - * Create relocation entry (PC relative doesn't need this).
  1749. - */
  1750. - if (relocation_needed) {
  1751. - if (verbose)
  1752. - printf(" RELOC[%d]: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  1753. - "section=%s size=%d "
  1754. - "fixup=0x%x (reloc=0x%"BFD_VMA_FMT"x)\n",
  1755. - flat_reloc_count,
  1756. - q->address, sym_name, addstr,
  1757. - section_name, sym_reloc_size,
  1758. - sym_addr, section_vma + q->address);
  1759. -
  1760. -#ifndef TARGET_bfin
  1761. - flat_relocs = realloc(flat_relocs,
  1762. - (flat_reloc_count + 1) * sizeof(uint32_t));
  1763. -#ifndef TARGET_e1
  1764. - flat_relocs[flat_reloc_count] = pflags |
  1765. - (section_vma + q->address);
  1766. -
  1767. -#else
  1768. - switch ((*p)->howto->type) {
  1769. - case R_E1_CONST31:
  1770. - case R_E1_CONST31_PCREL:
  1771. - case R_E1_DIS29W_PCREL:
  1772. - case R_E1_DIS29W:
  1773. - case R_E1_DIS29H:
  1774. - case R_E1_DIS29B:
  1775. - case R_E1_IMM32_PCREL:
  1776. - case R_E1_IMM32:
  1777. - flat_relocs[flat_reloc_count] = pflags |
  1778. - (section_vma + q->address + OPCODE_SIZE);
  1779. - if (verbose)
  1780. - printf("RELOCATION TABLE : reloc[%d] = [0x%x]\n", flat_reloc_count,
  1781. - flat_relocs[flat_reloc_count] );
  1782. - break;
  1783. - case R_E1_WORD:
  1784. - flat_relocs[flat_reloc_count] = pflags |
  1785. - (section_vma + q->address);
  1786. - if (verbose)
  1787. - printf("RELOCATION TABLE : reloc[%d] = [0x%x]\n", flat_reloc_count,
  1788. - flat_relocs[flat_reloc_count] );
  1789. - break;
  1790. - }
  1791. -#endif
  1792. - flat_reloc_count++;
  1793. -#endif //TARGET_bfin
  1794. - relocation_needed = 0;
  1795. - pflags = 0;
  1796. - }
  1797. -
  1798. -#if 0
  1799. -printf("%s(%d): symbol name=%s address=0x%x section=%s -> RELOC=0x%x\n",
  1800. - __FILE__, __LINE__, sym_name, q->address, section_name,
  1801. - flat_relocs[flat_reloc_count]);
  1802. -#endif
  1803. - }
  1804. - }
  1805. - }
  1806. -
  1807. - if (bad_relocs)
  1808. - fatal("%d bad relocs", bad_relocs);
  1809. -
  1810. - if (rc < 0)
  1811. - return(0);
  1812. -
  1813. - *n_relocs = flat_reloc_count;
  1814. - return flat_relocs;
  1815. -}
  1816. -
  1817. -
  1818. -
  1819. -static void usage(int status)
  1820. -{
  1821. - fprintf(status ? stderr : stdout,
  1822. - "Usage: %s [vrzd] [-p <abs-pic-file>] [-s stack-size] "
  1823. - "[-o <output-file>] <elf-file>\n\n"
  1824. - " -v : verbose operation\n"
  1825. - " -r : force load to RAM\n"
  1826. - " -k : enable kernel trace on load (for debug)\n"
  1827. - " -z : compress code/data/relocs\n"
  1828. - " -d : compress data/relocs\n"
  1829. - " -a : use existing symbol references\n"
  1830. - " instead of recalculating from\n"
  1831. - " relocation info\n"
  1832. - " -R reloc-file : read relocations from a separate file\n"
  1833. - " -p abs-pic-file : GOT/PIC processing with files\n"
  1834. - " -s stacksize : set application stack size\n"
  1835. - " -o output-file : output file name\n\n"
  1836. - "Compiled for " ARCH " architecture\n\n",
  1837. - elf2flt_progname);
  1838. - exit(status);
  1839. -}
  1840. -
  1841. -
  1842. -/* Write NUM zeroes to STREAM. */
  1843. -static void write_zeroes (unsigned long num, stream *stream)
  1844. -{
  1845. - char zeroes[1024];
  1846. - if (num > 0) {
  1847. - /* It'd be nice if we could just use fseek, but that doesn't seem to
  1848. - work for stdio output files. */
  1849. - memset(zeroes, 0x00, 1024);
  1850. - while (num > sizeof(zeroes)) {
  1851. - fwrite_stream(zeroes, sizeof(zeroes), 1, stream);
  1852. - num -= sizeof(zeroes);
  1853. - }
  1854. - if (num > 0)
  1855. - fwrite_stream(zeroes, num, 1, stream);
  1856. - }
  1857. -}
  1858. -
  1859. -
  1860. -static time_t get_build_date(void)
  1861. -{
  1862. - const char *sde;
  1863. - unsigned long long epoch;
  1864. - char *endptr;
  1865. -
  1866. - sde = getenv("SOURCE_DATE_EPOCH");
  1867. - if (!sde)
  1868. - return time(NULL);
  1869. -
  1870. - /* Largely inspired from
  1871. - https://reproducible-builds.org/docs/source-date-epoch/ */
  1872. - errno = 0;
  1873. - epoch = strtoull(sde, &endptr, 10);
  1874. - if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0))
  1875. - || (errno != 0 && epoch == 0)
  1876. - || (endptr == sde)
  1877. - || (*endptr != '\0')
  1878. - || (epoch > ULONG_MAX))
  1879. - fatal("Invalid SOURCE_DATE_EPOCH value");
  1880. -
  1881. - return (time_t)epoch;
  1882. -}
  1883. -
  1884. -
  1885. -int main(int argc, char *argv[])
  1886. -{
  1887. - int fd;
  1888. - bfd *rel_bfd, *abs_bfd;
  1889. - asection *s;
  1890. - char *ofile=NULL, *pfile=NULL, *abs_file = NULL, *rel_file = NULL;
  1891. - char *fname = NULL;
  1892. - int opt;
  1893. - int i;
  1894. - int stack;
  1895. - stream gf;
  1896. -
  1897. - asymbol **symbol_table;
  1898. - long number_of_symbols;
  1899. -
  1900. - uint32_t data_len = 0;
  1901. - uint32_t bss_len = 0;
  1902. - uint32_t text_len = 0;
  1903. - uint32_t reloc_len;
  1904. -
  1905. - uint32_t data_vma = ~0;
  1906. - uint32_t bss_vma = ~0;
  1907. - uint32_t text_vma = ~0;
  1908. -
  1909. - uint32_t text_offs;
  1910. -
  1911. - void *text;
  1912. - void *data;
  1913. - uint32_t *reloc;
  1914. -
  1915. - struct flat_hdr hdr;
  1916. -
  1917. - elf2flt_progname = argv[0];
  1918. - xmalloc_set_program_name(elf2flt_progname);
  1919. -
  1920. - if (argc < 2)
  1921. - usage(1);
  1922. -
  1923. - if (sizeof(hdr) != 64)
  1924. - fatal(
  1925. - "Potential flat header incompatibility detected\n"
  1926. - "header size should be 64 but is %d",
  1927. - sizeof(hdr));
  1928. -
  1929. -#ifndef TARGET_e1
  1930. - stack = 4096;
  1931. -#else /* We need plenty of stack for both of them (Aggregate and Register) */
  1932. - stack = 0x2020;
  1933. -#endif
  1934. -
  1935. - while ((opt = getopt(argc, argv, "havzdrkp:s:o:R:")) != -1) {
  1936. - switch (opt) {
  1937. - case 'v':
  1938. - verbose++;
  1939. - break;
  1940. - case 'r':
  1941. - load_to_ram++;
  1942. - break;
  1943. - case 'k':
  1944. - ktrace++;
  1945. - break;
  1946. - case 'z':
  1947. - docompress = 1;
  1948. - break;
  1949. - case 'd':
  1950. - docompress = 2;
  1951. - break;
  1952. - case 'p':
  1953. - pfile = optarg;
  1954. - break;
  1955. - case 'o':
  1956. - ofile = optarg;
  1957. - break;
  1958. - case 'a':
  1959. - use_resolved = 1;
  1960. - break;
  1961. - case 's':
  1962. - if (sscanf(optarg, "%i", &stack) != 1) {
  1963. - fprintf(stderr, "%s invalid stack size %s\n", argv[0], optarg);
  1964. - usage(1);
  1965. - }
  1966. - break;
  1967. - case 'R':
  1968. - rel_file = optarg;
  1969. - break;
  1970. - case 'h':
  1971. - usage(0);
  1972. - break;
  1973. - default:
  1974. - fprintf(stderr, "%s Unknown option\n", argv[0]);
  1975. - usage(1);
  1976. - break;
  1977. - }
  1978. - }
  1979. -
  1980. - /*
  1981. - * if neither the -r or -p options was given, default to
  1982. - * a RAM load as that is the only option that makes sense.
  1983. - */
  1984. - if (!load_to_ram && !pfile)
  1985. - load_to_ram = 1;
  1986. -
  1987. - fname = argv[argc-1];
  1988. -
  1989. - if (pfile) {
  1990. - pic_with_got = 1;
  1991. - abs_file = pfile;
  1992. - } else
  1993. - abs_file = fname;
  1994. -
  1995. - if (! rel_file)
  1996. - rel_file = fname;
  1997. -
  1998. - if (!(rel_bfd = bfd_openr(rel_file, 0)))
  1999. - fatal_perror("Can't open '%s'", rel_file);
  2000. -
  2001. - if (bfd_check_format (rel_bfd, bfd_object) == 0)
  2002. - fatal("File is not an object file");
  2003. -
  2004. - if (abs_file == rel_file)
  2005. - abs_bfd = rel_bfd; /* one file does all */
  2006. - else {
  2007. - if (!(abs_bfd = bfd_openr(abs_file, 0)))
  2008. - fatal_perror("Can't open '%s'", abs_file);
  2009. -
  2010. - if (bfd_check_format (abs_bfd, bfd_object) == 0)
  2011. - fatal("File is not an object file");
  2012. - }
  2013. -
  2014. - if (! (bfd_get_file_flags(rel_bfd) & HAS_RELOC))
  2015. - fatal("%s: Input file contains no relocation info", rel_file);
  2016. -
  2017. - if (use_resolved && !(bfd_get_file_flags(abs_bfd) & EXEC_P))
  2018. - /* `Absolute' file is not absolute, so neither are address
  2019. - contained therein. */
  2020. - fatal("%s: `-a' option specified with non-fully-resolved input file",
  2021. - bfd_get_filename (abs_bfd));
  2022. -
  2023. - symbol_table = get_symbols(abs_bfd, &number_of_symbols);
  2024. -
  2025. - /* Group output sections into text, data, and bss, and calc their sizes. */
  2026. - for (s = abs_bfd->sections; s != NULL; s = s->next) {
  2027. - uint32_t *vma, *len;
  2028. - bfd_size_type sec_size;
  2029. - bfd_vma sec_vma;
  2030. -
  2031. - if ((s->flags & SEC_CODE) ||
  2032. - ro_reloc_data_section_should_be_in_text(s)) {
  2033. - vma = &text_vma;
  2034. - len = &text_len;
  2035. - } else if (s->flags & SEC_DATA) {
  2036. - vma = &data_vma;
  2037. - len = &data_len;
  2038. - } else if (s->flags & SEC_ALLOC) {
  2039. - vma = &bss_vma;
  2040. - len = &bss_len;
  2041. - } else
  2042. - continue;
  2043. -
  2044. - sec_size = elf2flt_bfd_section_size(s);
  2045. - sec_vma = elf2flt_bfd_section_vma(s);
  2046. -
  2047. - if (sec_vma < *vma) {
  2048. - if (*len > 0)
  2049. - *len += sec_vma - *vma;
  2050. - else
  2051. - *len = sec_size;
  2052. - *vma = sec_vma;
  2053. - } else if (sec_vma + sec_size > *vma + *len)
  2054. - *len = sec_vma + sec_size - *vma;
  2055. - }
  2056. -
  2057. - if (text_len == 0)
  2058. - fatal("%s: no .text section", abs_file);
  2059. -
  2060. - text = xmalloc(text_len);
  2061. -
  2062. - if (verbose)
  2063. - printf("TEXT -> vma=0x%x len=0x%x\n", text_vma, text_len);
  2064. -
  2065. - /* Read input sections destined for the text output segment.
  2066. - * Includes code sections, but also includes read-only relocation
  2067. - * data sections.*/
  2068. - for (s = abs_bfd->sections; s != NULL; s = s->next)
  2069. - if ((s->flags & SEC_CODE) ||
  2070. - ro_reloc_data_section_should_be_in_text(s))
  2071. - if (!bfd_get_section_contents(abs_bfd, s,
  2072. - text + (s->vma - text_vma), 0,
  2073. - elf2flt_bfd_section_size(s)))
  2074. - {
  2075. - fatal("read error section %s", s->name);
  2076. - }
  2077. -
  2078. - if (data_len == 0)
  2079. - fatal("%s: no .data section", abs_file);
  2080. - data = xmalloc(data_len);
  2081. -
  2082. - if (verbose)
  2083. - printf("DATA -> vma=0x%x len=0x%x\n", data_vma, data_len);
  2084. -
  2085. - if ((text_vma + text_len) != data_vma) {
  2086. - if ((text_vma + text_len) > data_vma)
  2087. - fatal("ERROR: text=0x%x overlaps data=0x%x ?", text_len, data_vma);
  2088. - if (verbose)
  2089. - printf("WARNING: data=0x%x does not directly follow text=0x%x\n",
  2090. - data_vma, text_len);
  2091. - text_len = data_vma - text_vma;
  2092. - }
  2093. -
  2094. - /* Read input sections destined for the data output segment.
  2095. - * Includes data sections, but not those read-only relocation
  2096. - * data sections already included in the text output section.*/
  2097. - for (s = abs_bfd->sections; s != NULL; s = s->next)
  2098. - if ((s->flags & SEC_DATA) &&
  2099. - !ro_reloc_data_section_should_be_in_text(s))
  2100. - if (!bfd_get_section_contents(abs_bfd, s,
  2101. - data + (s->vma - data_vma), 0,
  2102. - elf2flt_bfd_section_size(s)))
  2103. - {
  2104. - fatal("read error section %s", s->name);
  2105. - }
  2106. -
  2107. - if (bss_vma == ~0)
  2108. - bss_vma = data_vma + data_len;
  2109. -
  2110. - /* Put common symbols in bss. */
  2111. - bss_len += add_com_to_bss(symbol_table, number_of_symbols, bss_len);
  2112. -
  2113. - if (verbose)
  2114. - printf("BSS -> vma=0x%x len=0x%x\n", bss_vma, bss_len);
  2115. -
  2116. - if ((data_vma + data_len) != bss_vma) {
  2117. - if ((data_vma + data_len) > bss_vma)
  2118. - fatal("ERROR: text=0x%x + data=0x%x overlaps bss=0x%x ?", text_len,
  2119. - data_len, bss_vma);
  2120. - if (verbose)
  2121. - printf("WARNING: bss=0x%x does not directly follow text=0x%x + data=0x%x(0x%x)\n",
  2122. - bss_vma, text_len, data_len, text_len + data_len);
  2123. - data_len = bss_vma - data_vma;
  2124. - }
  2125. -
  2126. - reloc = (uint32_t *)
  2127. - output_relocs(abs_bfd, symbol_table, number_of_symbols, &reloc_len,
  2128. - text, text_len, text_vma, data, data_len, data_vma, rel_bfd);
  2129. -
  2130. - if (reloc == NULL && verbose)
  2131. - printf("No relocations in code!\n");
  2132. -
  2133. - text_offs = real_address_bits(text_vma);
  2134. -
  2135. - /* Fill in the binflt_flat header */
  2136. - memcpy(hdr.magic,"bFLT",4);
  2137. - hdr.rev = htonl(FLAT_VERSION);
  2138. - hdr.entry = htonl(sizeof(hdr) + bfd_get_start_address(abs_bfd));
  2139. - hdr.data_start = htonl(sizeof(hdr) + text_offs + text_len);
  2140. - hdr.data_end = htonl(sizeof(hdr) + text_offs + text_len +data_len);
  2141. - hdr.bss_end = htonl(sizeof(hdr) + text_offs + text_len +data_len+bss_len);
  2142. - hdr.stack_size = htonl(stack); /* FIXME */
  2143. - hdr.reloc_start = htonl(sizeof(hdr) + text_offs + text_len +data_len);
  2144. - hdr.reloc_count = htonl(reloc_len);
  2145. - hdr.flags = htonl(0
  2146. - | (load_to_ram || text_has_relocs ? FLAT_FLAG_RAM : 0)
  2147. - | (ktrace ? FLAT_FLAG_KTRACE : 0)
  2148. - | (pic_with_got ? FLAT_FLAG_GOTPIC : 0)
  2149. - | (docompress ? (docompress == 2 ? FLAT_FLAG_GZDATA : FLAT_FLAG_GZIP) : 0)
  2150. - );
  2151. - hdr.build_date = htonl((uint32_t)get_build_date());
  2152. - memset(hdr.filler, 0x00, sizeof(hdr.filler));
  2153. -
  2154. - for (i=0; i<reloc_len; i++) reloc[i] = htonl(reloc[i]);
  2155. -
  2156. - if (verbose) {
  2157. - printf("SIZE: .text=0x%04x, .data=0x%04x, .bss=0x%04x",
  2158. - text_len, data_len, bss_len);
  2159. - if (reloc)
  2160. - printf(", relocs=0x%04x", reloc_len);
  2161. - printf("\n");
  2162. - }
  2163. -
  2164. - if (!ofile) {
  2165. - ofile = xmalloc(strlen(fname) + 5 + 1); /* 5 to add suffix */
  2166. - strcpy(ofile, fname);
  2167. - strcat(ofile, ".bflt");
  2168. - }
  2169. -
  2170. - if ((fd = open (ofile, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, 0744)) < 0)
  2171. - fatal_perror("Can't open output file %s", ofile);
  2172. -
  2173. - if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
  2174. - fatal_perror("Couldn't write file %s", ofile);
  2175. - close(fd);
  2176. -
  2177. - if (fopen_stream_u(&gf, ofile, "a" BINARY_FILE_OPTS))
  2178. - fatal_perror("Can't open file %s for writing", ofile);
  2179. -
  2180. - if (docompress == 1)
  2181. - reopen_stream_compressed(&gf);
  2182. -
  2183. - /* Fill in any hole at the beginning of the text segment. */
  2184. - if (verbose)
  2185. - printf("ZERO before text len=0x%x\n", text_offs);
  2186. - write_zeroes(text_offs, &gf);
  2187. -
  2188. - /* Write the text segment. */
  2189. - fwrite_stream(text, text_len, 1, &gf);
  2190. -
  2191. - if (docompress == 2)
  2192. - reopen_stream_compressed(&gf);
  2193. -
  2194. - /* Write the data segment. */
  2195. - fwrite_stream(data, data_len, 1, &gf);
  2196. -
  2197. - if (reloc)
  2198. - fwrite_stream(reloc, reloc_len * 4, 1, &gf);
  2199. -
  2200. - fclose_stream(&gf);
  2201. -
  2202. - exit(0);
  2203. -}
  2204. -
  2205. -
  2206. -/*
  2207. - * this __MUST__ be at the VERY end of the file - do NOT move!!
  2208. - *
  2209. - * Local Variables:
  2210. - * c-basic-offset: 4
  2211. - * tab-width: 8
  2212. - * end:
  2213. - * vi: tabstop=8 shiftwidth=4 textwidth=79 noexpandtab
  2214. - */