pef.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /* PEF support for BFD.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /* PEF (Preferred Executable Format) is the binary file format for late
  17. classic Mac OS versions (before Darwin). It is supported by both m68k
  18. and PowerPc. It is also called CFM (Code Fragment Manager). */
  19. #include "sysdep.h"
  20. #include "safe-ctype.h"
  21. #include "pef.h"
  22. #include "pef-traceback.h"
  23. #include "bfd.h"
  24. #include "libbfd.h"
  25. #include "libiberty.h"
  26. #ifndef BFD_IO_FUNCS
  27. #define BFD_IO_FUNCS 0
  28. #endif
  29. #define bfd_pef_close_and_cleanup _bfd_generic_close_and_cleanup
  30. #define bfd_pef_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  31. #define bfd_pef_new_section_hook _bfd_generic_new_section_hook
  32. #define bfd_pef_bfd_is_local_label_name bfd_generic_is_local_label_name
  33. #define bfd_pef_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  34. #define bfd_pef_get_lineno _bfd_nosymbols_get_lineno
  35. #define bfd_pef_find_nearest_line _bfd_nosymbols_find_nearest_line
  36. #define bfd_pef_find_line _bfd_nosymbols_find_line
  37. #define bfd_pef_find_inliner_info _bfd_nosymbols_find_inliner_info
  38. #define bfd_pef_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  39. #define bfd_pef_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  40. #define bfd_pef_read_minisymbols _bfd_generic_read_minisymbols
  41. #define bfd_pef_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  42. #define bfd_pef_set_arch_mach _bfd_generic_set_arch_mach
  43. #define bfd_pef_get_section_contents _bfd_generic_get_section_contents
  44. #define bfd_pef_set_section_contents _bfd_generic_set_section_contents
  45. #define bfd_pef_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  46. #define bfd_pef_bfd_relax_section bfd_generic_relax_section
  47. #define bfd_pef_bfd_gc_sections bfd_generic_gc_sections
  48. #define bfd_pef_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  49. #define bfd_pef_bfd_merge_sections bfd_generic_merge_sections
  50. #define bfd_pef_bfd_is_group_section bfd_generic_is_group_section
  51. #define bfd_pef_bfd_group_name bfd_generic_group_name
  52. #define bfd_pef_bfd_discard_group bfd_generic_discard_group
  53. #define bfd_pef_section_already_linked _bfd_generic_section_already_linked
  54. #define bfd_pef_bfd_define_common_symbol bfd_generic_define_common_symbol
  55. #define bfd_pef_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  56. #define bfd_pef_bfd_define_start_stop bfd_generic_define_start_stop
  57. #define bfd_pef_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  58. #define bfd_pef_bfd_link_add_symbols _bfd_generic_link_add_symbols
  59. #define bfd_pef_bfd_link_just_syms _bfd_generic_link_just_syms
  60. #define bfd_pef_bfd_copy_link_hash_symbol_type \
  61. _bfd_generic_copy_link_hash_symbol_type
  62. #define bfd_pef_bfd_final_link _bfd_generic_final_link
  63. #define bfd_pef_bfd_link_split_section _bfd_generic_link_split_section
  64. #define bfd_pef_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  65. #define bfd_pef_bfd_link_check_relocs _bfd_generic_link_check_relocs
  66. static int
  67. bfd_pef_parse_traceback_table (bfd *abfd,
  68. asection *section,
  69. unsigned char *buf,
  70. size_t len,
  71. size_t pos,
  72. asymbol *sym,
  73. FILE *file)
  74. {
  75. struct traceback_table table;
  76. size_t offset;
  77. const char *s;
  78. asymbol tmpsymbol;
  79. if (sym == NULL)
  80. sym = & tmpsymbol;
  81. sym->name = NULL;
  82. sym->value = 0;
  83. sym->the_bfd = abfd;
  84. sym->section = section;
  85. sym->flags = 0;
  86. sym->udata.i = 0;
  87. /* memcpy is fine since all fields are unsigned char. */
  88. if ((pos + 8) > len)
  89. return -1;
  90. memcpy (&table, buf + pos, 8);
  91. /* Calling code relies on returned symbols having a name and
  92. correct offset. */
  93. if ((table.lang != TB_C) && (table.lang != TB_CPLUSPLUS))
  94. return -1;
  95. if (! (table.flags2 & TB_NAME_PRESENT))
  96. return -1;
  97. if (! (table.flags1 & TB_HAS_TBOFF))
  98. return -1;
  99. offset = 8;
  100. if ((table.flags5 & TB_FLOATPARAMS) || (table.fixedparams))
  101. offset += 4;
  102. if (table.flags1 & TB_HAS_TBOFF)
  103. {
  104. struct traceback_table_tboff off;
  105. if ((pos + offset + 4) > len)
  106. return -1;
  107. off.tb_offset = bfd_getb32 (buf + pos + offset);
  108. offset += 4;
  109. /* Need to subtract 4 because the offset includes the 0x0L
  110. preceding the table. */
  111. if (file != NULL)
  112. fprintf (file, " [offset = 0x%lx]", off.tb_offset);
  113. if ((file == NULL) && ((off.tb_offset + 4) > (pos + offset)))
  114. return -1;
  115. sym->value = pos - off.tb_offset - 4;
  116. }
  117. if (table.flags2 & TB_INT_HNDL)
  118. offset += 4;
  119. if (table.flags1 & TB_HAS_CTL)
  120. {
  121. struct traceback_table_anchors anchors;
  122. if ((pos + offset + 4) > len)
  123. return -1;
  124. anchors.ctl_info = bfd_getb32 (buf + pos + offset);
  125. offset += 4;
  126. if (anchors.ctl_info > 1024)
  127. return -1;
  128. offset += anchors.ctl_info * 4;
  129. }
  130. if (table.flags2 & TB_NAME_PRESENT)
  131. {
  132. struct traceback_table_routine name;
  133. char *namebuf;
  134. if ((pos + offset + 2) > len)
  135. return -1;
  136. name.name_len = bfd_getb16 (buf + pos + offset);
  137. offset += 2;
  138. if (name.name_len > 4096)
  139. return -1;
  140. if ((pos + offset + name.name_len) > len)
  141. return -1;
  142. namebuf = bfd_alloc (abfd, name.name_len + 1);
  143. if (namebuf == NULL)
  144. return -1;
  145. memcpy (namebuf, buf + pos + offset, name.name_len);
  146. namebuf[name.name_len] = '\0';
  147. /* Strip leading period inserted by compiler. */
  148. if (namebuf[0] == '.')
  149. memmove (namebuf, namebuf + 1, name.name_len + 1);
  150. sym->name = namebuf;
  151. for (s = sym->name; (*s != '\0'); s++)
  152. if (! ISPRINT (*s))
  153. return -1;
  154. offset += name.name_len;
  155. }
  156. if (table.flags2 & TB_USES_ALLOCA)
  157. offset += 4;
  158. if (table.flags4 & TB_HAS_VEC_INFO)
  159. offset += 4;
  160. if (file != NULL)
  161. fprintf (file, " [length = 0x%lx]", (unsigned long) offset);
  162. return offset;
  163. }
  164. static void
  165. bfd_pef_print_symbol (bfd *abfd,
  166. void * afile,
  167. asymbol *symbol,
  168. bfd_print_symbol_type how)
  169. {
  170. FILE *file = (FILE *) afile;
  171. switch (how)
  172. {
  173. case bfd_print_symbol_name:
  174. fprintf (file, "%s", symbol->name);
  175. break;
  176. default:
  177. bfd_print_symbol_vandf (abfd, (void *) file, symbol);
  178. fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
  179. if (startswith (symbol->name, "__traceback_"))
  180. {
  181. unsigned char *buf;
  182. size_t offset = symbol->value + 4;
  183. size_t len = symbol->udata.i;
  184. buf = bfd_malloc (len);
  185. if (buf == NULL
  186. || !bfd_get_section_contents (abfd, symbol->section, buf,
  187. offset, len)
  188. || bfd_pef_parse_traceback_table (abfd, symbol->section, buf,
  189. len, 0, NULL, file) < 0)
  190. fprintf (file, " [ERROR]");
  191. free (buf);
  192. }
  193. }
  194. }
  195. static void
  196. bfd_pef_convert_architecture (unsigned long architecture,
  197. enum bfd_architecture *type,
  198. unsigned long *subtype)
  199. {
  200. const unsigned long ARCH_POWERPC = 0x70777063; /* 'pwpc'. */
  201. const unsigned long ARCH_M68K = 0x6d36386b; /* 'm68k'. */
  202. *subtype = bfd_arch_unknown;
  203. *type = bfd_arch_unknown;
  204. if (architecture == ARCH_POWERPC)
  205. *type = bfd_arch_powerpc;
  206. else if (architecture == ARCH_M68K)
  207. *type = bfd_arch_m68k;
  208. }
  209. static bool
  210. bfd_pef_mkobject (bfd *abfd ATTRIBUTE_UNUSED)
  211. {
  212. return true;
  213. }
  214. static const char *bfd_pef_section_name (bfd_pef_section *section)
  215. {
  216. switch (section->section_kind)
  217. {
  218. case BFD_PEF_SECTION_CODE: return "code";
  219. case BFD_PEF_SECTION_UNPACKED_DATA: return "unpacked-data";
  220. case BFD_PEF_SECTION_PACKED_DATA: return "packed-data";
  221. case BFD_PEF_SECTION_CONSTANT: return "constant";
  222. case BFD_PEF_SECTION_LOADER: return "loader";
  223. case BFD_PEF_SECTION_DEBUG: return "debug";
  224. case BFD_PEF_SECTION_EXEC_DATA: return "exec-data";
  225. case BFD_PEF_SECTION_EXCEPTION: return "exception";
  226. case BFD_PEF_SECTION_TRACEBACK: return "traceback";
  227. default: return "unknown";
  228. }
  229. }
  230. static unsigned long bfd_pef_section_flags (bfd_pef_section *section)
  231. {
  232. switch (section->section_kind)
  233. {
  234. case BFD_PEF_SECTION_CODE:
  235. return SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
  236. case BFD_PEF_SECTION_UNPACKED_DATA:
  237. case BFD_PEF_SECTION_PACKED_DATA:
  238. case BFD_PEF_SECTION_CONSTANT:
  239. case BFD_PEF_SECTION_LOADER:
  240. case BFD_PEF_SECTION_DEBUG:
  241. case BFD_PEF_SECTION_EXEC_DATA:
  242. case BFD_PEF_SECTION_EXCEPTION:
  243. case BFD_PEF_SECTION_TRACEBACK:
  244. default:
  245. return SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
  246. }
  247. }
  248. static asection *
  249. bfd_pef_make_bfd_section (bfd *abfd, bfd_pef_section *section)
  250. {
  251. asection *bfdsec;
  252. const char *name = bfd_pef_section_name (section);
  253. bfdsec = bfd_make_section_anyway (abfd, name);
  254. if (bfdsec == NULL)
  255. return NULL;
  256. bfdsec->vma = section->default_address + section->container_offset;
  257. bfdsec->lma = section->default_address + section->container_offset;
  258. bfdsec->size = section->container_length;
  259. bfdsec->filepos = section->container_offset;
  260. bfdsec->alignment_power = section->alignment;
  261. bfdsec->flags = bfd_pef_section_flags (section);
  262. return bfdsec;
  263. }
  264. int
  265. bfd_pef_parse_loader_header (bfd *abfd ATTRIBUTE_UNUSED,
  266. unsigned char *buf,
  267. size_t len,
  268. bfd_pef_loader_header *header)
  269. {
  270. BFD_ASSERT (len == 56);
  271. header->main_section = bfd_getb32 (buf);
  272. header->main_offset = bfd_getb32 (buf + 4);
  273. header->init_section = bfd_getb32 (buf + 8);
  274. header->init_offset = bfd_getb32 (buf + 12);
  275. header->term_section = bfd_getb32 (buf + 16);
  276. header->term_offset = bfd_getb32 (buf + 20);
  277. header->imported_library_count = bfd_getb32 (buf + 24);
  278. header->total_imported_symbol_count = bfd_getb32 (buf + 28);
  279. header->reloc_section_count = bfd_getb32 (buf + 32);
  280. header->reloc_instr_offset = bfd_getb32 (buf + 36);
  281. header->loader_strings_offset = bfd_getb32 (buf + 40);
  282. header->export_hash_offset = bfd_getb32 (buf + 44);
  283. header->export_hash_table_power = bfd_getb32 (buf + 48);
  284. header->exported_symbol_count = bfd_getb32 (buf + 52);
  285. return 0;
  286. }
  287. int
  288. bfd_pef_parse_imported_library (bfd *abfd ATTRIBUTE_UNUSED,
  289. unsigned char *buf,
  290. size_t len,
  291. bfd_pef_imported_library *header)
  292. {
  293. BFD_ASSERT (len == 24);
  294. header->name_offset = bfd_getb32 (buf);
  295. header->old_implementation_version = bfd_getb32 (buf + 4);
  296. header->current_version = bfd_getb32 (buf + 8);
  297. header->imported_symbol_count = bfd_getb32 (buf + 12);
  298. header->first_imported_symbol = bfd_getb32 (buf + 16);
  299. header->options = buf[20];
  300. header->reserved_a = buf[21];
  301. header->reserved_b = bfd_getb16 (buf + 22);
  302. return 0;
  303. }
  304. int
  305. bfd_pef_parse_imported_symbol (bfd *abfd ATTRIBUTE_UNUSED,
  306. unsigned char *buf,
  307. size_t len,
  308. bfd_pef_imported_symbol *symbol)
  309. {
  310. unsigned long value;
  311. BFD_ASSERT (len == 4);
  312. value = bfd_getb32 (buf);
  313. symbol->symbol_class = value >> 24;
  314. symbol->name = value & 0x00ffffff;
  315. return 0;
  316. }
  317. int
  318. bfd_pef_scan_section (bfd *abfd, bfd_pef_section *section)
  319. {
  320. unsigned char buf[28];
  321. bfd_seek (abfd, section->header_offset, SEEK_SET);
  322. if (bfd_bread ((void *) buf, 28, abfd) != 28)
  323. return -1;
  324. section->name_offset = bfd_h_get_32 (abfd, buf);
  325. section->default_address = bfd_h_get_32 (abfd, buf + 4);
  326. section->total_length = bfd_h_get_32 (abfd, buf + 8);
  327. section->unpacked_length = bfd_h_get_32 (abfd, buf + 12);
  328. section->container_length = bfd_h_get_32 (abfd, buf + 16);
  329. section->container_offset = bfd_h_get_32 (abfd, buf + 20);
  330. section->section_kind = buf[24];
  331. section->share_kind = buf[25];
  332. section->alignment = buf[26];
  333. section->reserved = buf[27];
  334. section->bfd_section = bfd_pef_make_bfd_section (abfd, section);
  335. if (section->bfd_section == NULL)
  336. return -1;
  337. return 0;
  338. }
  339. void
  340. bfd_pef_print_loader_header (bfd *abfd ATTRIBUTE_UNUSED,
  341. bfd_pef_loader_header *header,
  342. FILE *file)
  343. {
  344. fprintf (file, "main_section: %ld\n", header->main_section);
  345. fprintf (file, "main_offset: %lu\n", header->main_offset);
  346. fprintf (file, "init_section: %ld\n", header->init_section);
  347. fprintf (file, "init_offset: %lu\n", header->init_offset);
  348. fprintf (file, "term_section: %ld\n", header->term_section);
  349. fprintf (file, "term_offset: %lu\n", header->term_offset);
  350. fprintf (file, "imported_library_count: %lu\n",
  351. header->imported_library_count);
  352. fprintf (file, "total_imported_symbol_count: %lu\n",
  353. header->total_imported_symbol_count);
  354. fprintf (file, "reloc_section_count: %lu\n", header->reloc_section_count);
  355. fprintf (file, "reloc_instr_offset: %lu\n", header->reloc_instr_offset);
  356. fprintf (file, "loader_strings_offset: %lu\n",
  357. header->loader_strings_offset);
  358. fprintf (file, "export_hash_offset: %lu\n", header->export_hash_offset);
  359. fprintf (file, "export_hash_table_power: %lu\n",
  360. header->export_hash_table_power);
  361. fprintf (file, "exported_symbol_count: %lu\n",
  362. header->exported_symbol_count);
  363. }
  364. int
  365. bfd_pef_print_loader_section (bfd *abfd, FILE *file)
  366. {
  367. bfd_pef_loader_header header;
  368. asection *loadersec = NULL;
  369. unsigned char *loaderbuf = NULL;
  370. size_t loaderlen = 0;
  371. loadersec = bfd_get_section_by_name (abfd, "loader");
  372. if (loadersec == NULL)
  373. return -1;
  374. loaderlen = loadersec->size;
  375. if (loaderlen < 56)
  376. return -1;
  377. if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) != 0)
  378. return -1;
  379. loaderbuf = _bfd_malloc_and_read (abfd, loaderlen, loaderlen);
  380. if (loaderbuf == NULL)
  381. return -1;
  382. if (bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header) < 0)
  383. {
  384. free (loaderbuf);
  385. return -1;
  386. }
  387. bfd_pef_print_loader_header (abfd, &header, file);
  388. return 0;
  389. }
  390. int
  391. bfd_pef_scan_start_address (bfd *abfd)
  392. {
  393. bfd_pef_loader_header header;
  394. asection *section;
  395. asection *loadersec = NULL;
  396. unsigned char *loaderbuf = NULL;
  397. size_t loaderlen = 0;
  398. int ret;
  399. loadersec = bfd_get_section_by_name (abfd, "loader");
  400. if (loadersec == NULL)
  401. goto end;
  402. loaderlen = loadersec->size;
  403. if (loaderlen < 56)
  404. goto error;
  405. if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) != 0)
  406. goto error;
  407. loaderbuf = _bfd_malloc_and_read (abfd, loaderlen, loaderlen);
  408. if (loaderbuf == NULL)
  409. goto error;
  410. ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
  411. if (ret < 0)
  412. goto error;
  413. if (header.main_section < 0)
  414. goto end;
  415. for (section = abfd->sections; section != NULL; section = section->next)
  416. if ((long) (section->index + 1) == header.main_section)
  417. break;
  418. if (section == NULL)
  419. goto error;
  420. abfd->start_address = section->vma + header.main_offset;
  421. end:
  422. free (loaderbuf);
  423. return 0;
  424. error:
  425. free (loaderbuf);
  426. return -1;
  427. }
  428. int
  429. bfd_pef_scan (bfd *abfd,
  430. bfd_pef_header *header,
  431. bfd_pef_data_struct *mdata)
  432. {
  433. unsigned int i;
  434. enum bfd_architecture cputype;
  435. unsigned long cpusubtype;
  436. mdata->header = *header;
  437. bfd_pef_convert_architecture (header->architecture, &cputype, &cpusubtype);
  438. if (cputype == bfd_arch_unknown)
  439. {
  440. _bfd_error_handler (_("bfd_pef_scan: unknown architecture 0x%lx"),
  441. header->architecture);
  442. return -1;
  443. }
  444. bfd_set_arch_mach (abfd, cputype, cpusubtype);
  445. mdata->header = *header;
  446. abfd->flags = (abfd->xvec->object_flags
  447. | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
  448. if (header->section_count != 0)
  449. {
  450. mdata->sections = bfd_alloc (abfd, header->section_count * sizeof (bfd_pef_section));
  451. if (mdata->sections == NULL)
  452. return -1;
  453. for (i = 0; i < header->section_count; i++)
  454. {
  455. bfd_pef_section *cur = &mdata->sections[i];
  456. cur->header_offset = 40 + (i * 28);
  457. if (bfd_pef_scan_section (abfd, cur) < 0)
  458. return -1;
  459. }
  460. }
  461. if (bfd_pef_scan_start_address (abfd) < 0)
  462. return -1;
  463. abfd->tdata.pef_data = mdata;
  464. return 0;
  465. }
  466. static int
  467. bfd_pef_read_header (bfd *abfd, bfd_pef_header *header)
  468. {
  469. unsigned char buf[40];
  470. bfd_seek (abfd, 0, SEEK_SET);
  471. if (bfd_bread ((void *) buf, 40, abfd) != 40)
  472. return -1;
  473. header->tag1 = bfd_getb32 (buf);
  474. header->tag2 = bfd_getb32 (buf + 4);
  475. header->architecture = bfd_getb32 (buf + 8);
  476. header->format_version = bfd_getb32 (buf + 12);
  477. header->timestamp = bfd_getb32 (buf + 16);
  478. header->old_definition_version = bfd_getb32 (buf + 20);
  479. header->old_implementation_version = bfd_getb32 (buf + 24);
  480. header->current_version = bfd_getb32 (buf + 28);
  481. header->section_count = bfd_getb32 (buf + 32) + 1;
  482. header->instantiated_section_count = bfd_getb32 (buf + 34);
  483. header->reserved = bfd_getb32 (buf + 36);
  484. return 0;
  485. }
  486. static bfd_cleanup
  487. bfd_pef_object_p (bfd *abfd)
  488. {
  489. bfd_pef_header header;
  490. bfd_pef_data_struct *mdata;
  491. if (bfd_pef_read_header (abfd, &header) != 0)
  492. goto wrong;
  493. if (header.tag1 != BFD_PEF_TAG1 || header.tag2 != BFD_PEF_TAG2)
  494. goto wrong;
  495. mdata = (bfd_pef_data_struct *) bfd_zalloc (abfd, sizeof (*mdata));
  496. if (mdata == NULL)
  497. goto fail;
  498. if (bfd_pef_scan (abfd, &header, mdata))
  499. goto wrong;
  500. return _bfd_no_cleanup;
  501. wrong:
  502. bfd_set_error (bfd_error_wrong_format);
  503. fail:
  504. return NULL;
  505. }
  506. static int
  507. bfd_pef_parse_traceback_tables (bfd *abfd,
  508. asection *sec,
  509. unsigned char *buf,
  510. size_t len,
  511. long *nsym,
  512. asymbol **csym)
  513. {
  514. char *name;
  515. asymbol function;
  516. asymbol traceback;
  517. const char *const tbprefix = "__traceback_";
  518. size_t tbnamelen;
  519. size_t pos = 0;
  520. unsigned long count = 0;
  521. int ret;
  522. for (;;)
  523. {
  524. /* We're reading symbols two at a time. */
  525. if (csym && ((csym[count] == NULL) || (csym[count + 1] == NULL)))
  526. break;
  527. pos += 3;
  528. pos -= (pos % 4);
  529. while ((pos + 4) <= len)
  530. {
  531. if (bfd_getb32 (buf + pos) == 0)
  532. break;
  533. pos += 4;
  534. }
  535. if ((pos + 4) > len)
  536. break;
  537. ret = bfd_pef_parse_traceback_table (abfd, sec, buf, len, pos + 4,
  538. &function, 0);
  539. if (ret < 0)
  540. {
  541. /* Skip over 0x0L to advance to next possible traceback table. */
  542. pos += 4;
  543. continue;
  544. }
  545. BFD_ASSERT (function.name != NULL);
  546. /* Don't bother to compute the name if we are just
  547. counting symbols. */
  548. if (csym)
  549. {
  550. tbnamelen = strlen (tbprefix) + strlen (function.name);
  551. name = bfd_alloc (abfd, tbnamelen + 1);
  552. if (name == NULL)
  553. {
  554. bfd_release (abfd, (void *) function.name);
  555. function.name = NULL;
  556. break;
  557. }
  558. snprintf (name, tbnamelen + 1, "%s%s", tbprefix, function.name);
  559. traceback.name = name;
  560. traceback.value = pos;
  561. traceback.the_bfd = abfd;
  562. traceback.section = sec;
  563. traceback.flags = 0;
  564. traceback.udata.i = ret;
  565. *(csym[count]) = function;
  566. *(csym[count + 1]) = traceback;
  567. }
  568. pos += ret;
  569. count += 2;
  570. }
  571. *nsym = count;
  572. return 0;
  573. }
  574. static int
  575. bfd_pef_parse_function_stub (bfd *abfd ATTRIBUTE_UNUSED,
  576. unsigned char *buf,
  577. size_t len,
  578. unsigned long *offset)
  579. {
  580. BFD_ASSERT (len == 24);
  581. if ((bfd_getb32 (buf) & 0xffff0000) != 0x81820000)
  582. return -1;
  583. if (bfd_getb32 (buf + 4) != 0x90410014)
  584. return -1;
  585. if (bfd_getb32 (buf + 8) != 0x800c0000)
  586. return -1;
  587. if (bfd_getb32 (buf + 12) != 0x804c0004)
  588. return -1;
  589. if (bfd_getb32 (buf + 16) != 0x7c0903a6)
  590. return -1;
  591. if (bfd_getb32 (buf + 20) != 0x4e800420)
  592. return -1;
  593. if (offset != NULL)
  594. *offset = (bfd_getb32 (buf) & 0x0000ffff) / 4;
  595. return 0;
  596. }
  597. static int
  598. bfd_pef_parse_function_stubs (bfd *abfd,
  599. asection *codesec,
  600. unsigned char *codebuf,
  601. size_t codelen,
  602. unsigned char *loaderbuf,
  603. size_t loaderlen,
  604. unsigned long *nsym,
  605. asymbol **csym)
  606. {
  607. const char *const sprefix = "__stub_";
  608. size_t codepos = 0;
  609. unsigned long count = 0;
  610. bfd_pef_loader_header header;
  611. bfd_pef_imported_library *libraries = NULL;
  612. bfd_pef_imported_symbol *imports = NULL;
  613. unsigned long i;
  614. int ret;
  615. if (loaderlen < 56)
  616. goto error;
  617. ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
  618. if (ret < 0)
  619. goto error;
  620. libraries = bfd_malloc
  621. (header.imported_library_count * sizeof (bfd_pef_imported_library));
  622. imports = bfd_malloc
  623. (header.total_imported_symbol_count * sizeof (bfd_pef_imported_symbol));
  624. if (libraries == NULL || imports == NULL)
  625. goto error;
  626. if (loaderlen < (56 + (header.imported_library_count * 24)))
  627. goto error;
  628. for (i = 0; i < header.imported_library_count; i++)
  629. {
  630. ret = bfd_pef_parse_imported_library
  631. (abfd, loaderbuf + 56 + (i * 24), 24, &libraries[i]);
  632. if (ret < 0)
  633. goto error;
  634. }
  635. if (loaderlen < (56 + (header.imported_library_count * 24)
  636. + (header.total_imported_symbol_count * 4)))
  637. goto error;
  638. for (i = 0; i < header.total_imported_symbol_count; i++)
  639. {
  640. ret = (bfd_pef_parse_imported_symbol
  641. (abfd,
  642. loaderbuf + 56 + (header.imported_library_count * 24) + (i * 4),
  643. 4, &imports[i]));
  644. if (ret < 0)
  645. goto error;
  646. }
  647. codepos = 0;
  648. for (;;)
  649. {
  650. asymbol sym;
  651. const char *symname;
  652. char *name;
  653. unsigned long sym_index;
  654. if (csym && (csym[count] == NULL))
  655. break;
  656. codepos += 3;
  657. codepos -= (codepos % 4);
  658. while ((codepos + 4) <= codelen)
  659. {
  660. if ((bfd_getb32 (codebuf + codepos) & 0xffff0000) == 0x81820000)
  661. break;
  662. codepos += 4;
  663. }
  664. if ((codepos + 24) > codelen)
  665. break;
  666. ret = bfd_pef_parse_function_stub (abfd, codebuf + codepos, 24, &sym_index);
  667. if (ret < 0)
  668. {
  669. codepos += 24;
  670. continue;
  671. }
  672. if (sym_index >= header.total_imported_symbol_count)
  673. {
  674. codepos += 24;
  675. continue;
  676. }
  677. {
  678. size_t max, namelen;
  679. const char *s;
  680. if (loaderlen < (header.loader_strings_offset + imports[sym_index].name))
  681. goto error;
  682. max = loaderlen - (header.loader_strings_offset + imports[sym_index].name);
  683. symname = (char *) loaderbuf;
  684. symname += header.loader_strings_offset + imports[sym_index].name;
  685. namelen = 0;
  686. for (s = symname; s < (symname + max); s++)
  687. {
  688. if (*s == '\0')
  689. break;
  690. if (! ISPRINT (*s))
  691. goto error;
  692. namelen++;
  693. }
  694. if (*s != '\0')
  695. goto error;
  696. name = bfd_alloc (abfd, strlen (sprefix) + namelen + 1);
  697. if (name == NULL)
  698. break;
  699. snprintf (name, strlen (sprefix) + namelen + 1, "%s%s",
  700. sprefix, symname);
  701. sym.name = name;
  702. }
  703. sym.value = codepos;
  704. sym.the_bfd = abfd;
  705. sym.section = codesec;
  706. sym.flags = 0;
  707. sym.udata.i = 0;
  708. codepos += 24;
  709. if (csym != NULL)
  710. *(csym[count]) = sym;
  711. count++;
  712. }
  713. goto end;
  714. end:
  715. free (libraries);
  716. free (imports);
  717. *nsym = count;
  718. return 0;
  719. error:
  720. free (libraries);
  721. free (imports);
  722. *nsym = count;
  723. return -1;
  724. }
  725. static long
  726. bfd_pef_parse_symbols (bfd *abfd, asymbol **csym)
  727. {
  728. unsigned long count = 0;
  729. asection *codesec = NULL;
  730. unsigned char *codebuf = NULL;
  731. size_t codelen = 0;
  732. asection *loadersec = NULL;
  733. unsigned char *loaderbuf = NULL;
  734. size_t loaderlen = 0;
  735. codesec = bfd_get_section_by_name (abfd, "code");
  736. if (codesec != NULL)
  737. {
  738. codelen = codesec->size;
  739. if (bfd_seek (abfd, codesec->filepos, SEEK_SET) != 0)
  740. goto end;
  741. codebuf = _bfd_malloc_and_read (abfd, codelen, codelen);
  742. if (codebuf == NULL)
  743. goto end;
  744. }
  745. loadersec = bfd_get_section_by_name (abfd, "loader");
  746. if (loadersec != NULL)
  747. {
  748. loaderlen = loadersec->size;
  749. if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) != 0)
  750. goto end;
  751. loaderbuf = _bfd_malloc_and_read (abfd, loaderlen, loaderlen);
  752. if (loaderbuf == NULL)
  753. goto end;
  754. }
  755. count = 0;
  756. if (codesec != NULL)
  757. {
  758. long ncount = 0;
  759. bfd_pef_parse_traceback_tables (abfd, codesec, codebuf, codelen,
  760. &ncount, csym);
  761. count += ncount;
  762. }
  763. if ((codesec != NULL) && (loadersec != NULL))
  764. {
  765. unsigned long ncount = 0;
  766. bfd_pef_parse_function_stubs
  767. (abfd, codesec, codebuf, codelen, loaderbuf, loaderlen, &ncount,
  768. (csym != NULL) ? (csym + count) : NULL);
  769. count += ncount;
  770. }
  771. if (csym != NULL)
  772. csym[count] = NULL;
  773. end:
  774. free (codebuf);
  775. free (loaderbuf);
  776. return count;
  777. }
  778. static long
  779. bfd_pef_count_symbols (bfd *abfd)
  780. {
  781. return bfd_pef_parse_symbols (abfd, NULL);
  782. }
  783. static long
  784. bfd_pef_get_symtab_upper_bound (bfd *abfd)
  785. {
  786. long nsyms = bfd_pef_count_symbols (abfd);
  787. if (nsyms < 0)
  788. return nsyms;
  789. return ((nsyms + 1) * sizeof (asymbol *));
  790. }
  791. static long
  792. bfd_pef_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  793. {
  794. long i;
  795. asymbol *syms;
  796. long ret;
  797. long nsyms = bfd_pef_count_symbols (abfd);
  798. if (nsyms < 0)
  799. return nsyms;
  800. syms = bfd_alloc (abfd, nsyms * sizeof (asymbol));
  801. if (syms == NULL)
  802. return -1;
  803. for (i = 0; i < nsyms; i++)
  804. alocation[i] = &syms[i];
  805. alocation[nsyms] = NULL;
  806. ret = bfd_pef_parse_symbols (abfd, alocation);
  807. if (ret != nsyms)
  808. return 0;
  809. return ret;
  810. }
  811. #define bfd_pef_make_empty_symbol _bfd_generic_make_empty_symbol
  812. static void
  813. bfd_pef_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
  814. asymbol *symbol,
  815. symbol_info *ret)
  816. {
  817. bfd_symbol_info (symbol, ret);
  818. }
  819. static int
  820. bfd_pef_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  821. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  822. {
  823. return 0;
  824. }
  825. const bfd_target pef_vec =
  826. {
  827. "pef", /* Name. */
  828. bfd_target_pef_flavour, /* Flavour. */
  829. BFD_ENDIAN_BIG, /* Byteorder. */
  830. BFD_ENDIAN_BIG, /* Header_byteorder. */
  831. (HAS_RELOC | EXEC_P | /* Object flags. */
  832. HAS_LINENO | HAS_DEBUG |
  833. HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  834. (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
  835. | SEC_ROM | SEC_HAS_CONTENTS), /* Section_flags. */
  836. 0, /* Symbol_leading_char. */
  837. ' ', /* AR_pad_char. */
  838. 16, /* AR_max_namelen. */
  839. 0, /* match priority. */
  840. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  841. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  842. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  843. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  844. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  845. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  846. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
  847. { /* bfd_check_format. */
  848. _bfd_dummy_target,
  849. bfd_pef_object_p, /* bfd_check_format. */
  850. _bfd_dummy_target,
  851. _bfd_dummy_target,
  852. },
  853. { /* bfd_set_format. */
  854. _bfd_bool_bfd_false_error,
  855. bfd_pef_mkobject,
  856. _bfd_bool_bfd_false_error,
  857. _bfd_bool_bfd_false_error,
  858. },
  859. { /* bfd_write_contents. */
  860. _bfd_bool_bfd_false_error,
  861. _bfd_bool_bfd_true,
  862. _bfd_bool_bfd_false_error,
  863. _bfd_bool_bfd_false_error,
  864. },
  865. BFD_JUMP_TABLE_GENERIC (bfd_pef),
  866. BFD_JUMP_TABLE_COPY (_bfd_generic),
  867. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  868. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  869. BFD_JUMP_TABLE_SYMBOLS (bfd_pef),
  870. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  871. BFD_JUMP_TABLE_WRITE (bfd_pef),
  872. BFD_JUMP_TABLE_LINK (bfd_pef),
  873. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  874. NULL,
  875. NULL
  876. };
  877. #define bfd_pef_xlib_close_and_cleanup _bfd_generic_close_and_cleanup
  878. #define bfd_pef_xlib_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  879. #define bfd_pef_xlib_new_section_hook _bfd_generic_new_section_hook
  880. #define bfd_pef_xlib_get_section_contents _bfd_generic_get_section_contents
  881. #define bfd_pef_xlib_set_section_contents _bfd_generic_set_section_contents
  882. #define bfd_pef_xlib_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  883. #define bfd_pef_xlib_set_section_contents_in_window _bfd_generic_set_section_contents_in_window
  884. static int
  885. bfd_pef_xlib_read_header (bfd *abfd, bfd_pef_xlib_header *header)
  886. {
  887. unsigned char buf[80];
  888. bfd_seek (abfd, 0, SEEK_SET);
  889. if (bfd_bread ((void *) buf, sizeof buf, abfd) != sizeof buf)
  890. return -1;
  891. header->tag1 = bfd_getb32 (buf);
  892. header->tag2 = bfd_getb32 (buf + 4);
  893. header->current_format = bfd_getb32 (buf + 8);
  894. header->container_strings_offset = bfd_getb32 (buf + 12);
  895. header->export_hash_offset = bfd_getb32 (buf + 16);
  896. header->export_key_offset = bfd_getb32 (buf + 20);
  897. header->export_symbol_offset = bfd_getb32 (buf + 24);
  898. header->export_names_offset = bfd_getb32 (buf + 28);
  899. header->export_hash_table_power = bfd_getb32 (buf + 32);
  900. header->exported_symbol_count = bfd_getb32 (buf + 36);
  901. header->frag_name_offset = bfd_getb32 (buf + 40);
  902. header->frag_name_length = bfd_getb32 (buf + 44);
  903. header->dylib_path_offset = bfd_getb32 (buf + 48);
  904. header->dylib_path_length = bfd_getb32 (buf + 52);
  905. header->cpu_family = bfd_getb32 (buf + 56);
  906. header->cpu_model = bfd_getb32 (buf + 60);
  907. header->date_time_stamp = bfd_getb32 (buf + 64);
  908. header->current_version = bfd_getb32 (buf + 68);
  909. header->old_definition_version = bfd_getb32 (buf + 72);
  910. header->old_implementation_version = bfd_getb32 (buf + 76);
  911. return 0;
  912. }
  913. static int
  914. bfd_pef_xlib_scan (bfd *abfd, bfd_pef_xlib_header *header)
  915. {
  916. bfd_pef_xlib_data_struct *mdata = NULL;
  917. mdata = bfd_alloc (abfd, sizeof (* mdata));
  918. if (mdata == NULL)
  919. return -1;
  920. mdata->header = *header;
  921. abfd->flags = (abfd->xvec->object_flags
  922. | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
  923. abfd->tdata.pef_xlib_data = mdata;
  924. return 0;
  925. }
  926. static bfd_cleanup
  927. bfd_pef_xlib_object_p (bfd *abfd)
  928. {
  929. bfd_pef_xlib_header header;
  930. if (bfd_pef_xlib_read_header (abfd, &header) != 0)
  931. {
  932. bfd_set_error (bfd_error_wrong_format);
  933. return NULL;
  934. }
  935. if ((header.tag1 != BFD_PEF_XLIB_TAG1)
  936. || ((header.tag2 != BFD_PEF_VLIB_TAG2)
  937. && (header.tag2 != BFD_PEF_BLIB_TAG2)))
  938. {
  939. bfd_set_error (bfd_error_wrong_format);
  940. return NULL;
  941. }
  942. if (bfd_pef_xlib_scan (abfd, &header) != 0)
  943. {
  944. bfd_set_error (bfd_error_wrong_format);
  945. return NULL;
  946. }
  947. return _bfd_no_cleanup;
  948. }
  949. const bfd_target pef_xlib_vec =
  950. {
  951. "pef-xlib", /* Name. */
  952. bfd_target_pef_xlib_flavour, /* Flavour. */
  953. BFD_ENDIAN_BIG, /* Byteorder */
  954. BFD_ENDIAN_BIG, /* Header_byteorder. */
  955. (HAS_RELOC | EXEC_P | /* Object flags. */
  956. HAS_LINENO | HAS_DEBUG |
  957. HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  958. (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
  959. | SEC_ROM | SEC_HAS_CONTENTS),/* Section_flags. */
  960. 0, /* Symbol_leading_char. */
  961. ' ', /* AR_pad_char. */
  962. 16, /* AR_max_namelen. */
  963. 0, /* match priority. */
  964. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  965. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  966. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  967. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  968. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  969. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  970. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
  971. { /* bfd_check_format. */
  972. _bfd_dummy_target,
  973. bfd_pef_xlib_object_p, /* bfd_check_format. */
  974. _bfd_dummy_target,
  975. _bfd_dummy_target,
  976. },
  977. { /* bfd_set_format. */
  978. _bfd_bool_bfd_false_error,
  979. bfd_pef_mkobject,
  980. _bfd_bool_bfd_false_error,
  981. _bfd_bool_bfd_false_error,
  982. },
  983. { /* bfd_write_contents. */
  984. _bfd_bool_bfd_false_error,
  985. _bfd_bool_bfd_true,
  986. _bfd_bool_bfd_false_error,
  987. _bfd_bool_bfd_false_error,
  988. },
  989. BFD_JUMP_TABLE_GENERIC (bfd_pef_xlib),
  990. BFD_JUMP_TABLE_COPY (_bfd_generic),
  991. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  992. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  993. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  994. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  995. BFD_JUMP_TABLE_WRITE (_bfd_nowrite),
  996. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  997. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  998. NULL,
  999. NULL
  1000. };