tekhex.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /* BFD backend for Extended Tektronix Hex Format objects.
  2. Copyright (C) 1992-2022 Free Software Foundation, Inc.
  3. Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* SUBSECTION
  18. Tektronix Hex Format handling
  19. DESCRIPTION
  20. Tek Hex records can hold symbols and data, but not
  21. relocations. Their main application is communication with
  22. devices like PROM programmers and ICE equipment.
  23. It seems that the sections are described as being really big,
  24. the example I have says that the text section is 0..ffffffff.
  25. BFD would barf with this, many apps would try to alloc 4GB to
  26. read in the file.
  27. Tex Hex may contain many sections, but the data which comes in
  28. has no tag saying which section it belongs to, so we create
  29. one section for each block of data, called "blknnnn" which we
  30. stick all the data into.
  31. TekHex may come out of order and there is no header, so an
  32. initial scan is required to discover the minimum and maximum
  33. addresses used to create the vma and size of the sections we
  34. create.
  35. We read in the data into pages of CHUNK_MASK+1 size and read
  36. them out from that whenever we need to.
  37. Any number of sections may be created for output, we save them
  38. up and output them when it's time to close the bfd.
  39. A TekHex record looks like:
  40. EXAMPLE
  41. %<block length><type><checksum><stuff><cr>
  42. DESCRIPTION
  43. Where
  44. o length
  45. is the number of bytes in the record not including the % sign.
  46. o type
  47. is one of:
  48. 3) symbol record
  49. 6) data record
  50. 8) termination record
  51. The data can come out of order, and may be discontigous. This is a
  52. serial protocol, so big files are unlikely, so we keep a list of 8k chunks. */
  53. #include "sysdep.h"
  54. #include "bfd.h"
  55. #include "libbfd.h"
  56. #include "libiberty.h"
  57. typedef struct
  58. {
  59. bfd_vma low;
  60. bfd_vma high;
  61. } addr_range_type;
  62. typedef struct tekhex_symbol_struct
  63. {
  64. asymbol symbol;
  65. struct tekhex_symbol_struct *prev;
  66. } tekhex_symbol_type;
  67. static const char digs[] = "0123456789ABCDEF";
  68. static char sum_block[256];
  69. #define NOT_HEX 20
  70. #define NIBBLE(x) hex_value(x)
  71. #define HEX(buffer) ((NIBBLE ((buffer)[0]) << 4) + NIBBLE ((buffer)[1]))
  72. #define ISHEX(x) hex_p(x)
  73. #define TOHEX(d, x) \
  74. (d)[1] = digs[(x) & 0xf]; \
  75. (d)[0] = digs[((x)>>4)&0xf];
  76. /* Here's an example
  77. %3A6C6480004E56FFFC4E717063B0AEFFFC6D0652AEFFFC60F24E5E4E75
  78. %1B3709T_SEGMENT1108FFFFFFFF
  79. %2B3AB9T_SEGMENT7Dgcc_compiled$1087hello$c10
  80. %373829T_SEGMENT80int$t1$r1$$214741080char$t2$r2$0$12710
  81. %373769T_SEGMENT80long$int$t3$r1$$1080unsigned$int$t4$10
  82. %373CA9T_SEGMENT80long$unsigned$in1080short$int$t6$r1$10
  83. %373049T_SEGMENT80long$long$int$t71080short$unsigned$i10
  84. %373A29T_SEGMENT80long$long$unsign1080signed$char$t10$10
  85. %373D69T_SEGMENT80unsigned$char$t11080float$t12$r1$4$010
  86. %373D19T_SEGMENT80double$t13$r1$8$1080long$double$t14$10
  87. %2734D9T_SEGMENT8Bvoid$t15$151035_main10
  88. %2F3CA9T_SEGMENT81$1081$1681$1E81$21487main$F110
  89. %2832F9T_SEGMENT83i$18FFFFFFFC81$1481$214
  90. %07 8 10 10
  91. explanation:
  92. %3A6C6480004E56FFFC4E717063B0AEFFFC6D0652AEFFFC60F24E5E4E75
  93. ^ ^^ ^ ^-data
  94. | || +------ 4 char integer 0x8000
  95. | |+-------- checksum
  96. | +--------- type 6 (data record)
  97. +----------- length 3a chars
  98. <---------------------- 3a (58 chars) ------------------->
  99. %1B3709T_SEGMENT1108FFFFFFFF
  100. ^ ^^ ^- 8 character integer 0xffffffff
  101. | |+- 1 character integer 0
  102. | +-- type 1 symbol (section definition)
  103. +------------ 9 char symbol T_SEGMENT
  104. %2B3AB9T_SEGMENT7Dgcc_compiled$1087hello$c10
  105. %373829T_SEGMENT80int$t1$r1$$214741080char$t2$r2$0$12710
  106. %373769T_SEGMENT80long$int$t3$r1$$1080unsigned$int$t4$10
  107. %373CA9T_SEGMENT80long$unsigned$in1080short$int$t6$r1$10
  108. %373049T_SEGMENT80long$long$int$t71080short$unsigned$i10
  109. %373A29T_SEGMENT80long$long$unsign1080signed$char$t10$10
  110. %373D69T_SEGMENT80unsigned$char$t11080float$t12$r1$4$010
  111. %373D19T_SEGMENT80double$t13$r1$8$1080long$double$t14$10
  112. %2734D9T_SEGMENT8Bvoid$t15$151035_main10
  113. %2F3CA9T_SEGMENT81$1081$1681$1E81$21487main$F110
  114. %2832F9T_SEGMENT83i$18FFFFFFFC81$1481$214
  115. %0781010
  116. Turns into
  117. sac@thepub$ ./objdump -dx -m m68k f
  118. f: file format tekhex
  119. -----x--- 9/55728 -134219416 Sep 29 15:13 1995 f
  120. architecture: UNKNOWN!, flags 0x00000010:
  121. HAS_SYMS
  122. start address 0x00000000
  123. SECTION 0 [D00000000] : size 00020000 vma 00000000 align 2**0
  124. ALLOC, LOAD
  125. SECTION 1 [D00008000] : size 00002001 vma 00008000 align 2**0
  126. SECTION 2 [T_SEGMENT] : size ffffffff vma 00000000 align 2**0
  127. SYMBOL TABLE:
  128. 00000000 g T_SEGMENT gcc_compiled$
  129. 00000000 g T_SEGMENT hello$c
  130. 00000000 g T_SEGMENT int$t1$r1$$21474
  131. 00000000 g T_SEGMENT char$t2$r2$0$127
  132. 00000000 g T_SEGMENT long$int$t3$r1$$
  133. 00000000 g T_SEGMENT unsigned$int$t4$
  134. 00000000 g T_SEGMENT long$unsigned$in
  135. 00000000 g T_SEGMENT short$int$t6$r1$
  136. 00000000 g T_SEGMENT long$long$int$t7
  137. 00000000 g T_SEGMENT short$unsigned$i
  138. 00000000 g T_SEGMENT long$long$unsign
  139. 00000000 g T_SEGMENT signed$char$t10$
  140. 00000000 g T_SEGMENT unsigned$char$t1
  141. 00000000 g T_SEGMENT float$t12$r1$4$0
  142. 00000000 g T_SEGMENT double$t13$r1$8$
  143. 00000000 g T_SEGMENT long$double$t14$
  144. 00000000 g T_SEGMENT void$t15$15
  145. 00000000 g T_SEGMENT _main
  146. 00000000 g T_SEGMENT $
  147. 00000000 g T_SEGMENT $
  148. 00000000 g T_SEGMENT $
  149. 00000010 g T_SEGMENT $
  150. 00000000 g T_SEGMENT main$F1
  151. fcffffff g T_SEGMENT i$1
  152. 00000000 g T_SEGMENT $
  153. 00000010 g T_SEGMENT $
  154. RELOCATION RECORDS FOR [D00000000]: (none)
  155. RELOCATION RECORDS FOR [D00008000]: (none)
  156. RELOCATION RECORDS FOR [T_SEGMENT]: (none)
  157. Disassembly of section D00000000:
  158. ...
  159. 00008000 ($+)7ff0 linkw fp,#-4
  160. 00008004 ($+)7ff4 nop
  161. 00008006 ($+)7ff6 movel #99,d0
  162. 00008008 ($+)7ff8 cmpl fp@(-4),d0
  163. 0000800c ($+)7ffc blts 00008014 ($+)8004
  164. 0000800e ($+)7ffe addql #1,fp@(-4)
  165. 00008012 ($+)8002 bras 00008006 ($+)7ff6
  166. 00008014 ($+)8004 unlk fp
  167. 00008016 ($+)8006 rts
  168. ... */
  169. static void
  170. tekhex_init (void)
  171. {
  172. unsigned int i;
  173. static bool inited = false;
  174. int val;
  175. if (! inited)
  176. {
  177. inited = true;
  178. hex_init ();
  179. val = 0;
  180. for (i = 0; i < 10; i++)
  181. sum_block[i + '0'] = val++;
  182. for (i = 'A'; i <= 'Z'; i++)
  183. sum_block[i] = val++;
  184. sum_block['$'] = val++;
  185. sum_block['%'] = val++;
  186. sum_block['.'] = val++;
  187. sum_block['_'] = val++;
  188. for (i = 'a'; i <= 'z'; i++)
  189. sum_block[i] = val++;
  190. }
  191. }
  192. /* The maximum number of bytes on a line is FF. */
  193. #define MAXCHUNK 0xff
  194. /* The number of bytes we fit onto a line on output. */
  195. #define CHUNK 21
  196. /* We cannot output our tekhexords as we see them, we have to glue them
  197. together, this is done in this structure : */
  198. struct tekhex_data_list_struct
  199. {
  200. unsigned char *data;
  201. bfd_vma where;
  202. bfd_size_type size;
  203. struct tekhex_data_list_struct *next;
  204. };
  205. typedef struct tekhex_data_list_struct tekhex_data_list_type;
  206. #define CHUNK_MASK 0x1fff
  207. #define CHUNK_SPAN 32
  208. struct data_struct
  209. {
  210. unsigned char chunk_data[CHUNK_MASK + 1];
  211. unsigned char chunk_init[(CHUNK_MASK + 1 + CHUNK_SPAN - 1) / CHUNK_SPAN];
  212. bfd_vma vma;
  213. struct data_struct *next;
  214. };
  215. typedef struct tekhex_data_struct
  216. {
  217. tekhex_data_list_type *head;
  218. unsigned int type;
  219. struct tekhex_symbol_struct *symbols;
  220. struct data_struct *data;
  221. } tdata_type;
  222. #define enda(x) (x->vma + x->size)
  223. static bool
  224. getvalue (char **srcp, bfd_vma *valuep, char * endp)
  225. {
  226. char *src = *srcp;
  227. bfd_vma value = 0;
  228. unsigned int len;
  229. if (src >= endp)
  230. return false;
  231. if (!ISHEX (*src))
  232. return false;
  233. len = hex_value (*src++);
  234. if (len == 0)
  235. len = 16;
  236. while (len-- && src < endp)
  237. {
  238. if (!ISHEX (*src))
  239. return false;
  240. value = value << 4 | hex_value (*src++);
  241. }
  242. *srcp = src;
  243. *valuep = value;
  244. return len == -1U;
  245. }
  246. static bool
  247. getsym (char *dstp, char **srcp, unsigned int *lenp, char * endp)
  248. {
  249. char *src = *srcp;
  250. unsigned int i;
  251. unsigned int len;
  252. if (!ISHEX (*src))
  253. return false;
  254. len = hex_value (*src++);
  255. if (len == 0)
  256. len = 16;
  257. for (i = 0; i < len && (src + i) < endp; i++)
  258. dstp[i] = src[i];
  259. dstp[i] = 0;
  260. *srcp = src + i;
  261. *lenp = len;
  262. return i == len;
  263. }
  264. static struct data_struct *
  265. find_chunk (bfd *abfd, bfd_vma vma, bool create)
  266. {
  267. struct data_struct *d = abfd->tdata.tekhex_data->data;
  268. vma &= ~CHUNK_MASK;
  269. while (d && (d->vma) != vma)
  270. d = d->next;
  271. if (!d && create)
  272. {
  273. /* No chunk for this address, so make one up. */
  274. d = (struct data_struct *)
  275. bfd_zalloc (abfd, (bfd_size_type) sizeof (struct data_struct));
  276. if (!d)
  277. return NULL;
  278. d->next = abfd->tdata.tekhex_data->data;
  279. d->vma = vma;
  280. abfd->tdata.tekhex_data->data = d;
  281. }
  282. return d;
  283. }
  284. static void
  285. insert_byte (bfd *abfd, int value, bfd_vma addr)
  286. {
  287. if (value != 0)
  288. {
  289. /* Find the chunk that this byte needs and put it in. */
  290. struct data_struct *d = find_chunk (abfd, addr, true);
  291. d->chunk_data[addr & CHUNK_MASK] = value;
  292. d->chunk_init[(addr & CHUNK_MASK) / CHUNK_SPAN] = 1;
  293. }
  294. }
  295. /* The first pass is to find the names of all the sections, and see
  296. how big the data is. */
  297. static bool
  298. first_phase (bfd *abfd, int type, char *src, char * src_end)
  299. {
  300. asection *section, *alt_section;
  301. unsigned int len;
  302. bfd_vma val;
  303. char sym[17]; /* A symbol can only be 16chars long. */
  304. switch (type)
  305. {
  306. case '6':
  307. /* Data record - read it and store it. */
  308. {
  309. bfd_vma addr;
  310. if (!getvalue (&src, &addr, src_end))
  311. return false;
  312. while (*src && src < src_end - 1)
  313. {
  314. insert_byte (abfd, HEX (src), addr);
  315. src += 2;
  316. addr++;
  317. }
  318. return true;
  319. }
  320. case '3':
  321. /* Symbol record, read the segment. */
  322. if (!getsym (sym, &src, &len, src_end))
  323. return false;
  324. section = bfd_get_section_by_name (abfd, sym);
  325. if (section == NULL)
  326. {
  327. char *n = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1);
  328. if (!n)
  329. return false;
  330. memcpy (n, sym, len + 1);
  331. section = bfd_make_section (abfd, n);
  332. if (section == NULL)
  333. return false;
  334. }
  335. alt_section = NULL;
  336. while (src < src_end && *src)
  337. {
  338. switch (*src)
  339. {
  340. case '1': /* Section range. */
  341. src++;
  342. if (!getvalue (&src, &section->vma, src_end))
  343. return false;
  344. if (!getvalue (&src, &val, src_end))
  345. return false;
  346. if (val < section->vma)
  347. val = section->vma;
  348. section->size = val - section->vma;
  349. /* PR 17512: file: objdump-s-endless-loop.tekhex.
  350. Check for overlarge section sizes. */
  351. if (section->size & 0x80000000)
  352. return false;
  353. section->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
  354. break;
  355. case '0':
  356. case '2':
  357. case '3':
  358. case '4':
  359. case '6':
  360. case '7':
  361. case '8':
  362. /* Symbols, add to section. */
  363. {
  364. size_t amt = sizeof (tekhex_symbol_type);
  365. tekhex_symbol_type *new_symbol = (tekhex_symbol_type *)
  366. bfd_alloc (abfd, amt);
  367. char stype = (*src);
  368. if (!new_symbol)
  369. return false;
  370. new_symbol->symbol.the_bfd = abfd;
  371. src++;
  372. abfd->symcount++;
  373. abfd->flags |= HAS_SYMS;
  374. new_symbol->prev = abfd->tdata.tekhex_data->symbols;
  375. abfd->tdata.tekhex_data->symbols = new_symbol;
  376. if (!getsym (sym, &src, &len, src_end))
  377. return false;
  378. new_symbol->symbol.name = (const char *)
  379. bfd_alloc (abfd, (bfd_size_type) len + 1);
  380. if (!new_symbol->symbol.name)
  381. return false;
  382. memcpy ((char *) (new_symbol->symbol.name), sym, len + 1);
  383. new_symbol->symbol.section = section;
  384. if (stype <= '4')
  385. new_symbol->symbol.flags = (BSF_GLOBAL | BSF_EXPORT);
  386. else
  387. new_symbol->symbol.flags = BSF_LOCAL;
  388. if (stype == '2' || stype == '6')
  389. new_symbol->symbol.section = bfd_abs_section_ptr;
  390. else if (stype == '3' || stype == '7')
  391. {
  392. if ((section->flags & SEC_DATA) == 0)
  393. section->flags |= SEC_CODE;
  394. else
  395. {
  396. if (alt_section == NULL)
  397. alt_section
  398. = bfd_get_next_section_by_name (NULL, section);
  399. if (alt_section == NULL)
  400. alt_section = bfd_make_section_anyway_with_flags
  401. (abfd, section->name,
  402. (section->flags & ~SEC_DATA) | SEC_CODE);
  403. if (alt_section == NULL)
  404. return false;
  405. new_symbol->symbol.section = alt_section;
  406. }
  407. }
  408. else if (stype == '4' || stype == '8')
  409. {
  410. if ((section->flags & SEC_CODE) == 0)
  411. section->flags |= SEC_DATA;
  412. else
  413. {
  414. if (alt_section == NULL)
  415. alt_section
  416. = bfd_get_next_section_by_name (NULL, section);
  417. if (alt_section == NULL)
  418. alt_section = bfd_make_section_anyway_with_flags
  419. (abfd, section->name,
  420. (section->flags & ~SEC_CODE) | SEC_DATA);
  421. if (alt_section == NULL)
  422. return false;
  423. new_symbol->symbol.section = alt_section;
  424. }
  425. }
  426. if (!getvalue (&src, &val, src_end))
  427. return false;
  428. new_symbol->symbol.value = val - section->vma;
  429. break;
  430. }
  431. default:
  432. return false;
  433. }
  434. }
  435. }
  436. return true;
  437. }
  438. /* Pass over a tekhex, calling one of the above functions on each
  439. record. */
  440. static bool
  441. pass_over (bfd *abfd, bool (*func) (bfd *, int, char *, char *))
  442. {
  443. unsigned int chars_on_line;
  444. bool is_eof = false;
  445. /* To the front of the file. */
  446. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  447. return false;
  448. while (! is_eof)
  449. {
  450. char src[MAXCHUNK];
  451. char type;
  452. /* Find first '%'. */
  453. is_eof = (bool) (bfd_bread (src, (bfd_size_type) 1, abfd) != 1);
  454. while (!is_eof && *src != '%')
  455. is_eof = (bool) (bfd_bread (src, (bfd_size_type) 1, abfd) != 1);
  456. if (is_eof)
  457. break;
  458. /* Fetch the type and the length and the checksum. */
  459. if (bfd_bread (src, (bfd_size_type) 5, abfd) != 5)
  460. return false;
  461. type = src[2];
  462. if (!ISHEX (src[0]) || !ISHEX (src[1]))
  463. break;
  464. /* Already read five chars. */
  465. chars_on_line = HEX (src) - 5;
  466. if (chars_on_line >= MAXCHUNK)
  467. return false;
  468. if (bfd_bread (src, (bfd_size_type) chars_on_line, abfd) != chars_on_line)
  469. return false;
  470. /* Put a null at the end. */
  471. src[chars_on_line] = 0;
  472. if (!func (abfd, type, src, src + chars_on_line))
  473. return false;
  474. }
  475. return true;
  476. }
  477. static long
  478. tekhex_canonicalize_symtab (bfd *abfd, asymbol **table)
  479. {
  480. tekhex_symbol_type *p = abfd->tdata.tekhex_data->symbols;
  481. unsigned int c = bfd_get_symcount (abfd);
  482. table[c] = 0;
  483. while (p)
  484. {
  485. table[--c] = &(p->symbol);
  486. p = p->prev;
  487. }
  488. return bfd_get_symcount (abfd);
  489. }
  490. static long
  491. tekhex_get_symtab_upper_bound (bfd *abfd)
  492. {
  493. return (abfd->symcount + 1) * (sizeof (struct tekhex_asymbol_struct *));
  494. }
  495. static bool
  496. tekhex_mkobject (bfd *abfd)
  497. {
  498. tdata_type *tdata;
  499. tdata = (tdata_type *) bfd_alloc (abfd, (bfd_size_type) sizeof (tdata_type));
  500. if (!tdata)
  501. return false;
  502. abfd->tdata.tekhex_data = tdata;
  503. tdata->type = 1;
  504. tdata->head = NULL;
  505. tdata->symbols = NULL;
  506. tdata->data = NULL;
  507. return true;
  508. }
  509. /* Return TRUE if the file looks like it's in TekHex format. Just look
  510. for a percent sign and some hex digits. */
  511. static bfd_cleanup
  512. tekhex_object_p (bfd *abfd)
  513. {
  514. char b[4];
  515. tekhex_init ();
  516. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  517. || bfd_bread (b, (bfd_size_type) 4, abfd) != 4)
  518. return NULL;
  519. if (b[0] != '%' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
  520. return NULL;
  521. tekhex_mkobject (abfd);
  522. if (!pass_over (abfd, first_phase))
  523. return NULL;
  524. return _bfd_no_cleanup;
  525. }
  526. static void
  527. move_section_contents (bfd *abfd,
  528. asection *section,
  529. const void * locationp,
  530. file_ptr offset,
  531. bfd_size_type count,
  532. bool get)
  533. {
  534. bfd_vma addr;
  535. char *location = (char *) locationp;
  536. bfd_vma prev_number = 1; /* Nothing can have this as a high bit. */
  537. struct data_struct *d = NULL;
  538. BFD_ASSERT (offset == 0);
  539. for (addr = section->vma; count != 0; count--, addr++)
  540. {
  541. /* Get high bits of address. */
  542. bfd_vma chunk_number = addr & ~(bfd_vma) CHUNK_MASK;
  543. bfd_vma low_bits = addr & CHUNK_MASK;
  544. bool must_write = !get && *location != 0;
  545. if (chunk_number != prev_number || (!d && must_write))
  546. {
  547. /* Different chunk, so move pointer. */
  548. d = find_chunk (abfd, chunk_number, must_write);
  549. prev_number = chunk_number;
  550. }
  551. if (get)
  552. {
  553. if (d)
  554. *location = d->chunk_data[low_bits];
  555. else
  556. *location = 0;
  557. }
  558. else if (must_write)
  559. {
  560. d->chunk_data[low_bits] = *location;
  561. d->chunk_init[low_bits / CHUNK_SPAN] = 1;
  562. }
  563. location++;
  564. }
  565. }
  566. static bool
  567. tekhex_get_section_contents (bfd *abfd,
  568. asection *section,
  569. void * locationp,
  570. file_ptr offset,
  571. bfd_size_type count)
  572. {
  573. if (section->flags & (SEC_LOAD | SEC_ALLOC))
  574. {
  575. move_section_contents (abfd, section, locationp, offset, count, true);
  576. return true;
  577. }
  578. return false;
  579. }
  580. static bool
  581. tekhex_set_arch_mach (bfd *abfd,
  582. enum bfd_architecture arch,
  583. unsigned long machine)
  584. {
  585. /* Ignore errors about unknown architecture. */
  586. return (bfd_default_set_arch_mach (abfd, arch, machine)
  587. || arch == bfd_arch_unknown);
  588. }
  589. /* We have to save up all the Tekhexords for a splurge before output. */
  590. static bool
  591. tekhex_set_section_contents (bfd *abfd,
  592. sec_ptr section,
  593. const void * locationp,
  594. file_ptr offset,
  595. bfd_size_type bytes_to_do)
  596. {
  597. if (section->flags & (SEC_LOAD | SEC_ALLOC))
  598. {
  599. move_section_contents (abfd, section, locationp, offset, bytes_to_do,
  600. false);
  601. return true;
  602. }
  603. return false;
  604. }
  605. static void
  606. writevalue (char **dst, bfd_vma value)
  607. {
  608. char *p = *dst;
  609. int len;
  610. int shift;
  611. for (len = 8, shift = 28; shift; shift -= 4, len--)
  612. {
  613. if ((value >> shift) & 0xf)
  614. {
  615. *p++ = len + '0';
  616. while (len)
  617. {
  618. *p++ = digs[(value >> shift) & 0xf];
  619. shift -= 4;
  620. len--;
  621. }
  622. *dst = p;
  623. return;
  624. }
  625. }
  626. *p++ = '1';
  627. *p++ = '0';
  628. *dst = p;
  629. }
  630. static void
  631. writesym (char **dst, const char *sym)
  632. {
  633. char *p = *dst;
  634. int len = (sym ? strlen (sym) : 0);
  635. if (len >= 16)
  636. {
  637. *p++ = '0';
  638. len = 16;
  639. }
  640. else
  641. {
  642. if (len == 0)
  643. {
  644. *p++ = '1';
  645. sym = "$";
  646. len = 1;
  647. }
  648. else
  649. *p++ = digs[len];
  650. }
  651. while (len--)
  652. *p++ = *sym++;
  653. *dst = p;
  654. }
  655. static void
  656. out (bfd *abfd, int type, char *start, char *end)
  657. {
  658. int sum = 0;
  659. char *s;
  660. char front[6];
  661. bfd_size_type wrlen;
  662. front[0] = '%';
  663. TOHEX (front + 1, end - start + 5);
  664. front[3] = type;
  665. for (s = start; s < end; s++)
  666. sum += sum_block[(unsigned char) *s];
  667. sum += sum_block[(unsigned char) front[1]]; /* Length. */
  668. sum += sum_block[(unsigned char) front[2]];
  669. sum += sum_block[(unsigned char) front[3]]; /* Type. */
  670. TOHEX (front + 4, sum);
  671. if (bfd_bwrite (front, (bfd_size_type) 6, abfd) != 6)
  672. abort ();
  673. end[0] = '\n';
  674. wrlen = end - start + 1;
  675. if (bfd_bwrite (start, wrlen, abfd) != wrlen)
  676. abort ();
  677. }
  678. static bool
  679. tekhex_write_object_contents (bfd *abfd)
  680. {
  681. char buffer[100];
  682. asymbol **p;
  683. asection *s;
  684. struct data_struct *d;
  685. tekhex_init ();
  686. /* And the raw data. */
  687. for (d = abfd->tdata.tekhex_data->data;
  688. d != NULL;
  689. d = d->next)
  690. {
  691. int low;
  692. int addr;
  693. /* Write it in blocks of 32 bytes. */
  694. for (addr = 0; addr < CHUNK_MASK + 1; addr += CHUNK_SPAN)
  695. {
  696. if (d->chunk_init[addr / CHUNK_SPAN])
  697. {
  698. char *dst = buffer;
  699. writevalue (&dst, addr + d->vma);
  700. for (low = 0; low < CHUNK_SPAN; low++)
  701. {
  702. TOHEX (dst, d->chunk_data[addr + low]);
  703. dst += 2;
  704. }
  705. out (abfd, '6', buffer, dst);
  706. }
  707. }
  708. }
  709. /* Write all the section headers for the sections. */
  710. for (s = abfd->sections; s != NULL; s = s->next)
  711. {
  712. char *dst = buffer;
  713. writesym (&dst, s->name);
  714. *dst++ = '1';
  715. writevalue (&dst, s->vma);
  716. writevalue (&dst, s->vma + s->size);
  717. out (abfd, '3', buffer, dst);
  718. }
  719. /* And the symbols. */
  720. if (abfd->outsymbols)
  721. {
  722. for (p = abfd->outsymbols; *p; p++)
  723. {
  724. int section_code = bfd_decode_symclass (*p);
  725. if (section_code != '?')
  726. {
  727. /* Do not include debug symbols. */
  728. asymbol *sym = *p;
  729. char *dst = buffer;
  730. writesym (&dst, sym->section->name);
  731. switch (section_code)
  732. {
  733. case 'A':
  734. *dst++ = '2';
  735. break;
  736. case 'a':
  737. *dst++ = '6';
  738. break;
  739. case 'D':
  740. case 'B':
  741. case 'O':
  742. *dst++ = '4';
  743. break;
  744. case 'd':
  745. case 'b':
  746. case 'o':
  747. *dst++ = '8';
  748. break;
  749. case 'T':
  750. *dst++ = '3';
  751. break;
  752. case 't':
  753. *dst++ = '7';
  754. break;
  755. case 'C':
  756. case 'U':
  757. bfd_set_error (bfd_error_wrong_format);
  758. return false;
  759. }
  760. writesym (&dst, sym->name);
  761. writevalue (&dst, sym->value + sym->section->vma);
  762. out (abfd, '3', buffer, dst);
  763. }
  764. }
  765. }
  766. /* And the terminator. */
  767. if (bfd_bwrite ("%0781010\n", (bfd_size_type) 9, abfd) != 9)
  768. abort ();
  769. return true;
  770. }
  771. static int
  772. tekhex_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  773. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  774. {
  775. return 0;
  776. }
  777. static asymbol *
  778. tekhex_make_empty_symbol (bfd *abfd)
  779. {
  780. size_t amt = sizeof (struct tekhex_symbol_struct);
  781. tekhex_symbol_type *new_symbol = (tekhex_symbol_type *) bfd_zalloc (abfd,
  782. amt);
  783. if (!new_symbol)
  784. return NULL;
  785. new_symbol->symbol.the_bfd = abfd;
  786. new_symbol->prev = NULL;
  787. return &(new_symbol->symbol);
  788. }
  789. static void
  790. tekhex_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
  791. asymbol *symbol,
  792. symbol_info *ret)
  793. {
  794. bfd_symbol_info (symbol, ret);
  795. }
  796. static void
  797. tekhex_print_symbol (bfd *abfd,
  798. void * filep,
  799. asymbol *symbol,
  800. bfd_print_symbol_type how)
  801. {
  802. FILE *file = (FILE *) filep;
  803. switch (how)
  804. {
  805. case bfd_print_symbol_name:
  806. fprintf (file, "%s", symbol->name);
  807. break;
  808. case bfd_print_symbol_more:
  809. break;
  810. case bfd_print_symbol_all:
  811. {
  812. const char *section_name = symbol->section->name;
  813. bfd_print_symbol_vandf (abfd, (void *) file, symbol);
  814. fprintf (file, " %-5s %s",
  815. section_name, symbol->name);
  816. }
  817. }
  818. }
  819. #define tekhex_close_and_cleanup _bfd_generic_close_and_cleanup
  820. #define tekhex_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  821. #define tekhex_new_section_hook _bfd_generic_new_section_hook
  822. #define tekhex_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  823. #define tekhex_bfd_is_local_label_name bfd_generic_is_local_label_name
  824. #define tekhex_get_lineno _bfd_nosymbols_get_lineno
  825. #define tekhex_find_nearest_line _bfd_nosymbols_find_nearest_line
  826. #define tekhex_find_line _bfd_nosymbols_find_line
  827. #define tekhex_find_inliner_info _bfd_nosymbols_find_inliner_info
  828. #define tekhex_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  829. #define tekhex_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  830. #define tekhex_read_minisymbols _bfd_generic_read_minisymbols
  831. #define tekhex_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  832. #define tekhex_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  833. #define tekhex_bfd_relax_section bfd_generic_relax_section
  834. #define tekhex_bfd_gc_sections bfd_generic_gc_sections
  835. #define tekhex_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  836. #define tekhex_bfd_merge_sections bfd_generic_merge_sections
  837. #define tekhex_bfd_is_group_section bfd_generic_is_group_section
  838. #define tekhex_bfd_group_name bfd_generic_group_name
  839. #define tekhex_bfd_discard_group bfd_generic_discard_group
  840. #define tekhex_section_already_linked _bfd_generic_section_already_linked
  841. #define tekhex_bfd_define_common_symbol bfd_generic_define_common_symbol
  842. #define tekhex_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  843. #define tekhex_bfd_define_start_stop bfd_generic_define_start_stop
  844. #define tekhex_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  845. #define tekhex_bfd_link_add_symbols _bfd_generic_link_add_symbols
  846. #define tekhex_bfd_link_just_syms _bfd_generic_link_just_syms
  847. #define tekhex_bfd_copy_link_hash_symbol_type _bfd_generic_copy_link_hash_symbol_type
  848. #define tekhex_bfd_final_link _bfd_generic_final_link
  849. #define tekhex_bfd_link_split_section _bfd_generic_link_split_section
  850. #define tekhex_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  851. #define tekhex_bfd_link_check_relocs _bfd_generic_link_check_relocs
  852. const bfd_target tekhex_vec =
  853. {
  854. "tekhex", /* Name. */
  855. bfd_target_tekhex_flavour,
  856. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  857. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  858. (EXEC_P | /* Object flags. */
  859. HAS_SYMS | HAS_LINENO | HAS_DEBUG |
  860. HAS_RELOC | HAS_LOCALS | WP_TEXT | D_PAGED),
  861. (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  862. | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
  863. 0, /* Leading underscore. */
  864. ' ', /* AR_pad_char. */
  865. 16, /* AR_max_namelen. */
  866. 0, /* match priority. */
  867. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  868. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  869. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  870. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  871. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  872. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  873. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
  874. {
  875. _bfd_dummy_target,
  876. tekhex_object_p, /* bfd_check_format. */
  877. _bfd_dummy_target,
  878. _bfd_dummy_target,
  879. },
  880. {
  881. _bfd_bool_bfd_false_error,
  882. tekhex_mkobject,
  883. _bfd_generic_mkarchive,
  884. _bfd_bool_bfd_false_error,
  885. },
  886. { /* bfd_write_contents. */
  887. _bfd_bool_bfd_false_error,
  888. tekhex_write_object_contents,
  889. _bfd_write_archive_contents,
  890. _bfd_bool_bfd_false_error,
  891. },
  892. BFD_JUMP_TABLE_GENERIC (tekhex),
  893. BFD_JUMP_TABLE_COPY (_bfd_generic),
  894. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  895. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  896. BFD_JUMP_TABLE_SYMBOLS (tekhex),
  897. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  898. BFD_JUMP_TABLE_WRITE (tekhex),
  899. BFD_JUMP_TABLE_LINK (tekhex),
  900. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  901. NULL,
  902. NULL
  903. };