simple-object-mach-o.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /* simple-object-mach-o.c -- routines to manipulate Mach-O object files.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. 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, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "config.h"
  17. #include "libiberty.h"
  18. #include "simple-object.h"
  19. #include <stddef.h>
  20. #ifdef HAVE_STDLIB_H
  21. #include <stdlib.h>
  22. #endif
  23. #ifdef HAVE_STDINT_H
  24. #include <stdint.h>
  25. #endif
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #ifdef HAVE_INTTYPES_H
  30. #include <inttypes.h>
  31. #endif
  32. #include "simple-object-common.h"
  33. /* Mach-O structures and constants. */
  34. /* Mach-O header (32-bit version). */
  35. struct mach_o_header_32
  36. {
  37. unsigned char magic[4]; /* Magic number. */
  38. unsigned char cputype[4]; /* CPU that this object is for. */
  39. unsigned char cpusubtype[4]; /* CPU subtype. */
  40. unsigned char filetype[4]; /* Type of file. */
  41. unsigned char ncmds[4]; /* Number of load commands. */
  42. unsigned char sizeofcmds[4]; /* Total size of load commands. */
  43. unsigned char flags[4]; /* Flags for special featues. */
  44. };
  45. /* Mach-O header (64-bit version). */
  46. struct mach_o_header_64
  47. {
  48. unsigned char magic[4]; /* Magic number. */
  49. unsigned char cputype[4]; /* CPU that this object is for. */
  50. unsigned char cpusubtype[4]; /* CPU subtype. */
  51. unsigned char filetype[4]; /* Type of file. */
  52. unsigned char ncmds[4]; /* Number of load commands. */
  53. unsigned char sizeofcmds[4]; /* Total size of load commands. */
  54. unsigned char flags[4]; /* Flags for special featues. */
  55. unsigned char reserved[4]; /* Reserved. Duh. */
  56. };
  57. /* For magic field in header. */
  58. #define MACH_O_MH_MAGIC 0xfeedface
  59. #define MACH_O_MH_MAGIC_64 0xfeedfacf
  60. /* For filetype field in header. */
  61. #define MACH_O_MH_OBJECT 0x01
  62. /* A Mach-O file is a list of load commands. This is the header of a
  63. load command. */
  64. struct mach_o_load_command
  65. {
  66. unsigned char cmd[4]; /* The type of load command. */
  67. unsigned char cmdsize[4]; /* Size in bytes of entire command. */
  68. };
  69. /* For cmd field in load command. */
  70. #define MACH_O_LC_SEGMENT 0x01
  71. #define MACH_O_LC_SEGMENT_64 0x19
  72. /* LC_SEGMENT load command. */
  73. struct mach_o_segment_command_32
  74. {
  75. unsigned char cmd[4]; /* The type of load command (LC_SEGMENT). */
  76. unsigned char cmdsize[4]; /* Size in bytes of entire command. */
  77. unsigned char segname[16]; /* Name of this segment. */
  78. unsigned char vmaddr[4]; /* Virtual memory address of this segment. */
  79. unsigned char vmsize[4]; /* Size there, in bytes. */
  80. unsigned char fileoff[4]; /* Offset in bytes of the data to be mapped. */
  81. unsigned char filesize[4]; /* Size in bytes on disk. */
  82. unsigned char maxprot[4]; /* Maximum permitted vmem protection. */
  83. unsigned char initprot[4]; /* Initial vmem protection. */
  84. unsigned char nsects[4]; /* Number of sections in this segment. */
  85. unsigned char flags[4]; /* Flags that affect the loading. */
  86. };
  87. /* LC_SEGMENT_64 load command. */
  88. struct mach_o_segment_command_64
  89. {
  90. unsigned char cmd[4]; /* The type of load command (LC_SEGMENT_64). */
  91. unsigned char cmdsize[4]; /* Size in bytes of entire command. */
  92. unsigned char segname[16]; /* Name of this segment. */
  93. unsigned char vmaddr[8]; /* Virtual memory address of this segment. */
  94. unsigned char vmsize[8]; /* Size there, in bytes. */
  95. unsigned char fileoff[8]; /* Offset in bytes of the data to be mapped. */
  96. unsigned char filesize[8]; /* Size in bytes on disk. */
  97. unsigned char maxprot[4]; /* Maximum permitted vmem protection. */
  98. unsigned char initprot[4]; /* Initial vmem protection. */
  99. unsigned char nsects[4]; /* Number of sections in this segment. */
  100. unsigned char flags[4]; /* Flags that affect the loading. */
  101. };
  102. /* 32-bit section header. */
  103. struct mach_o_section_32
  104. {
  105. unsigned char sectname[16]; /* Section name. */
  106. unsigned char segname[16]; /* Segment that the section belongs to. */
  107. unsigned char addr[4]; /* Address of this section in memory. */
  108. unsigned char size[4]; /* Size in bytes of this section. */
  109. unsigned char offset[4]; /* File offset of this section. */
  110. unsigned char align[4]; /* log2 of this section's alignment. */
  111. unsigned char reloff[4]; /* File offset of this section's relocs. */
  112. unsigned char nreloc[4]; /* Number of relocs for this section. */
  113. unsigned char flags[4]; /* Section flags/attributes. */
  114. unsigned char reserved1[4];
  115. unsigned char reserved2[4];
  116. };
  117. /* 64-bit section header. */
  118. struct mach_o_section_64
  119. {
  120. unsigned char sectname[16]; /* Section name. */
  121. unsigned char segname[16]; /* Segment that the section belongs to. */
  122. unsigned char addr[8]; /* Address of this section in memory. */
  123. unsigned char size[8]; /* Size in bytes of this section. */
  124. unsigned char offset[4]; /* File offset of this section. */
  125. unsigned char align[4]; /* log2 of this section's alignment. */
  126. unsigned char reloff[4]; /* File offset of this section's relocs. */
  127. unsigned char nreloc[4]; /* Number of relocs for this section. */
  128. unsigned char flags[4]; /* Section flags/attributes. */
  129. unsigned char reserved1[4];
  130. unsigned char reserved2[4];
  131. unsigned char reserved3[4];
  132. };
  133. /* Flags for Mach-O sections. */
  134. #define MACH_O_S_ATTR_DEBUG 0x02000000
  135. /* The length of a segment or section name. */
  136. #define MACH_O_NAME_LEN (16)
  137. /* A GNU specific extension for long section names. */
  138. #define GNU_SECTION_NAMES "__section_names"
  139. /* A GNU-specific extension to wrap multiple sections using three
  140. mach-o sections within a given segment. The section '__wrapper_sects'
  141. is subdivided according to the index '__wrapper_index' and each sub
  142. sect is named according to the names supplied in '__wrapper_names'. */
  143. #define GNU_WRAPPER_SECTS "__wrapper_sects"
  144. #define GNU_WRAPPER_INDEX "__wrapper_index"
  145. #define GNU_WRAPPER_NAMES "__wrapper_names"
  146. /* Private data for an simple_object_read. */
  147. struct simple_object_mach_o_read
  148. {
  149. /* User specified segment name. */
  150. char *segment_name;
  151. /* Magic number. */
  152. unsigned int magic;
  153. /* Whether this file is big-endian. */
  154. int is_big_endian;
  155. /* CPU type from header. */
  156. unsigned int cputype;
  157. /* CPU subtype from header. */
  158. unsigned int cpusubtype;
  159. /* Number of commands, from header. */
  160. unsigned int ncmds;
  161. /* Flags from header. */
  162. unsigned int flags;
  163. /* Reserved field from header, only used on 64-bit. */
  164. unsigned int reserved;
  165. };
  166. /* Private data for an simple_object_attributes. */
  167. struct simple_object_mach_o_attributes
  168. {
  169. /* Magic number. */
  170. unsigned int magic;
  171. /* Whether this file is big-endian. */
  172. int is_big_endian;
  173. /* CPU type from header. */
  174. unsigned int cputype;
  175. /* CPU subtype from header. */
  176. unsigned int cpusubtype;
  177. /* Flags from header. */
  178. unsigned int flags;
  179. /* Reserved field from header, only used on 64-bit. */
  180. unsigned int reserved;
  181. };
  182. /* See if we have a Mach-O MH_OBJECT file:
  183. A standard MH_OBJECT (from as) will have three load commands:
  184. 0 - LC_SEGMENT/LC_SEGMENT64
  185. 1 - LC_SYMTAB
  186. 2 - LC_DYSYMTAB
  187. The LC_SEGMENT/LC_SEGMENT64 will introduce a single anonymous segment
  188. containing all the sections.
  189. Files written by simple-object will have only the segment command
  190. (no symbol tables). */
  191. static void *
  192. simple_object_mach_o_match (
  193. unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
  194. int descriptor,
  195. off_t offset,
  196. const char *segment_name,
  197. const char **errmsg,
  198. int *err)
  199. {
  200. unsigned int magic;
  201. int is_big_endian;
  202. unsigned int (*fetch_32) (const unsigned char *);
  203. unsigned int filetype;
  204. struct simple_object_mach_o_read *omr;
  205. unsigned char buf[sizeof (struct mach_o_header_64)];
  206. unsigned char *b;
  207. magic = simple_object_fetch_big_32 (header);
  208. if (magic == MACH_O_MH_MAGIC || magic == MACH_O_MH_MAGIC_64)
  209. is_big_endian = 1;
  210. else
  211. {
  212. magic = simple_object_fetch_little_32 (header);
  213. if (magic == MACH_O_MH_MAGIC || magic == MACH_O_MH_MAGIC_64)
  214. is_big_endian = 0;
  215. else
  216. {
  217. *errmsg = NULL;
  218. *err = 0;
  219. return NULL;
  220. }
  221. }
  222. #ifndef UNSIGNED_64BIT_TYPE
  223. if (magic == MACH_O_MH_MAGIC_64)
  224. {
  225. *errmsg = "64-bit Mach-O objects not supported";
  226. *err = 0;
  227. return NULL;
  228. }
  229. #endif
  230. /* We require the user to provide a segment name. This is
  231. unfortunate but I don't see any good choices here. */
  232. if (segment_name == NULL)
  233. {
  234. *errmsg = "Mach-O file found but no segment name specified";
  235. *err = 0;
  236. return NULL;
  237. }
  238. if (strlen (segment_name) > MACH_O_NAME_LEN)
  239. {
  240. *errmsg = "Mach-O segment name too long";
  241. *err = 0;
  242. return NULL;
  243. }
  244. /* The 32-bit and 64-bit headers are similar enough that we can use
  245. the same code. */
  246. fetch_32 = (is_big_endian
  247. ? simple_object_fetch_big_32
  248. : simple_object_fetch_little_32);
  249. if (!simple_object_internal_read (descriptor, offset, buf,
  250. (magic == MACH_O_MH_MAGIC
  251. ? sizeof (struct mach_o_header_32)
  252. : sizeof (struct mach_o_header_64)),
  253. errmsg, err))
  254. return NULL;
  255. b = &buf[0];
  256. filetype = (*fetch_32) (b + offsetof (struct mach_o_header_32, filetype));
  257. if (filetype != MACH_O_MH_OBJECT)
  258. {
  259. *errmsg = "Mach-O file is not object file";
  260. *err = 0;
  261. return NULL;
  262. }
  263. omr = XNEW (struct simple_object_mach_o_read);
  264. omr->segment_name = xstrdup (segment_name);
  265. omr->magic = magic;
  266. omr->is_big_endian = is_big_endian;
  267. omr->cputype = (*fetch_32) (b + offsetof (struct mach_o_header_32, cputype));
  268. omr->cpusubtype = (*fetch_32) (b
  269. + offsetof (struct mach_o_header_32,
  270. cpusubtype));
  271. omr->ncmds = (*fetch_32) (b + offsetof (struct mach_o_header_32, ncmds));
  272. omr->flags = (*fetch_32) (b + offsetof (struct mach_o_header_32, flags));
  273. if (magic == MACH_O_MH_MAGIC)
  274. omr->reserved = 0;
  275. else
  276. omr->reserved = (*fetch_32) (b
  277. + offsetof (struct mach_o_header_64,
  278. reserved));
  279. return (void *) omr;
  280. }
  281. /* Get the file offset and size from a section header. */
  282. static void
  283. simple_object_mach_o_section_info (int is_big_endian, int is_32,
  284. const unsigned char *sechdr, off_t *offset,
  285. size_t *size)
  286. {
  287. unsigned int (*fetch_32) (const unsigned char *);
  288. ulong_type (*fetch_64) (const unsigned char *);
  289. fetch_32 = (is_big_endian
  290. ? simple_object_fetch_big_32
  291. : simple_object_fetch_little_32);
  292. fetch_64 = NULL;
  293. #ifdef UNSIGNED_64BIT_TYPE
  294. fetch_64 = (is_big_endian
  295. ? simple_object_fetch_big_64
  296. : simple_object_fetch_little_64);
  297. #endif
  298. if (is_32)
  299. {
  300. *offset = fetch_32 (sechdr
  301. + offsetof (struct mach_o_section_32, offset));
  302. *size = fetch_32 (sechdr
  303. + offsetof (struct mach_o_section_32, size));
  304. }
  305. else
  306. {
  307. *offset = fetch_32 (sechdr
  308. + offsetof (struct mach_o_section_64, offset));
  309. *size = fetch_64 (sechdr
  310. + offsetof (struct mach_o_section_64, size));
  311. }
  312. }
  313. /* Handle a segment in a Mach-O Object file.
  314. This will callback to the function pfn for each "section found" the meaning
  315. of which depends on gnu extensions to mach-o:
  316. If we find mach-o sections (with the segment name as specified) which also
  317. contain: a 'sects' wrapper, an index, and a name table, we expand this into
  318. as many sections as are specified in the index. In this case, there will
  319. be a callback for each of these.
  320. We will also allow an extension that permits long names (more than 16
  321. characters) to be used with mach-o. In this case, the section name has
  322. a specific format embedding an index into a name table, and the file must
  323. contain such name table.
  324. Return 1 if we should continue, 0 if the caller should return. */
  325. #define SOMO_SECTS_PRESENT 0x01
  326. #define SOMO_INDEX_PRESENT 0x02
  327. #define SOMO_NAMES_PRESENT 0x04
  328. #define SOMO_LONGN_PRESENT 0x08
  329. #define SOMO_WRAPPING (SOMO_SECTS_PRESENT | SOMO_INDEX_PRESENT \
  330. | SOMO_NAMES_PRESENT)
  331. static int
  332. simple_object_mach_o_segment (simple_object_read *sobj, off_t offset,
  333. const unsigned char *segbuf,
  334. int (*pfn) (void *, const char *, off_t offset,
  335. off_t length),
  336. void *data,
  337. const char **errmsg, int *err)
  338. {
  339. struct simple_object_mach_o_read *omr =
  340. (struct simple_object_mach_o_read *) sobj->data;
  341. unsigned int (*fetch_32) (const unsigned char *);
  342. int is_32;
  343. size_t seghdrsize;
  344. size_t sechdrsize;
  345. size_t segname_offset;
  346. size_t sectname_offset;
  347. unsigned int nsects;
  348. unsigned char *secdata;
  349. unsigned int i;
  350. unsigned int gnu_sections_found;
  351. unsigned int strtab_index;
  352. unsigned int index_index;
  353. unsigned int nametab_index;
  354. unsigned int sections_index;
  355. char *strtab;
  356. char *nametab;
  357. unsigned char *index;
  358. size_t strtab_size;
  359. size_t nametab_size;
  360. size_t index_size;
  361. unsigned int n_wrapped_sects;
  362. size_t wrapper_sect_size;
  363. off_t wrapper_sect_offset = 0;
  364. fetch_32 = (omr->is_big_endian
  365. ? simple_object_fetch_big_32
  366. : simple_object_fetch_little_32);
  367. is_32 = omr->magic == MACH_O_MH_MAGIC;
  368. if (is_32)
  369. {
  370. seghdrsize = sizeof (struct mach_o_segment_command_32);
  371. sechdrsize = sizeof (struct mach_o_section_32);
  372. segname_offset = offsetof (struct mach_o_section_32, segname);
  373. sectname_offset = offsetof (struct mach_o_section_32, sectname);
  374. nsects = (*fetch_32) (segbuf
  375. + offsetof (struct mach_o_segment_command_32,
  376. nsects));
  377. }
  378. else
  379. {
  380. seghdrsize = sizeof (struct mach_o_segment_command_64);
  381. sechdrsize = sizeof (struct mach_o_section_64);
  382. segname_offset = offsetof (struct mach_o_section_64, segname);
  383. sectname_offset = offsetof (struct mach_o_section_64, sectname);
  384. nsects = (*fetch_32) (segbuf
  385. + offsetof (struct mach_o_segment_command_64,
  386. nsects));
  387. }
  388. /* Fetch the section headers from the segment command. */
  389. secdata = XNEWVEC (unsigned char, nsects * sechdrsize);
  390. if (!simple_object_internal_read (sobj->descriptor, offset + seghdrsize,
  391. secdata, nsects * sechdrsize, errmsg, err))
  392. {
  393. XDELETEVEC (secdata);
  394. return 0;
  395. }
  396. /* Scan for special sections that signal GNU extensions to the format. */
  397. gnu_sections_found = 0;
  398. index_index = nsects;
  399. sections_index = nsects;
  400. strtab_index = nsects;
  401. nametab_index = nsects;
  402. for (i = 0; i < nsects; ++i)
  403. {
  404. size_t nameoff;
  405. nameoff = i * sechdrsize + segname_offset;
  406. if (strcmp ((char *) secdata + nameoff, omr->segment_name) != 0)
  407. continue;
  408. nameoff = i * sechdrsize + sectname_offset;
  409. if (strcmp ((char *) secdata + nameoff, GNU_WRAPPER_NAMES) == 0)
  410. {
  411. nametab_index = i;
  412. gnu_sections_found |= SOMO_NAMES_PRESENT;
  413. }
  414. else if (strcmp ((char *) secdata + nameoff, GNU_WRAPPER_INDEX) == 0)
  415. {
  416. index_index = i;
  417. gnu_sections_found |= SOMO_INDEX_PRESENT;
  418. }
  419. else if (strcmp ((char *) secdata + nameoff, GNU_WRAPPER_SECTS) == 0)
  420. {
  421. sections_index = i;
  422. gnu_sections_found |= SOMO_SECTS_PRESENT;
  423. }
  424. else if (strcmp ((char *) secdata + nameoff, GNU_SECTION_NAMES) == 0)
  425. {
  426. strtab_index = i;
  427. gnu_sections_found |= SOMO_LONGN_PRESENT;
  428. }
  429. }
  430. /* If any of the special wrapper section components is present, then
  431. they all should be. */
  432. if ((gnu_sections_found & SOMO_WRAPPING) != 0)
  433. {
  434. off_t nametab_offset;
  435. off_t index_offset;
  436. if ((gnu_sections_found & SOMO_WRAPPING) != SOMO_WRAPPING)
  437. {
  438. *errmsg = "GNU Mach-o section wrapper: required section missing";
  439. *err = 0; /* No useful errno. */
  440. XDELETEVEC (secdata);
  441. return 0;
  442. }
  443. /* Fetch the name table. */
  444. simple_object_mach_o_section_info (omr->is_big_endian, is_32,
  445. secdata + nametab_index * sechdrsize,
  446. &nametab_offset, &nametab_size);
  447. nametab = XNEWVEC (char, nametab_size);
  448. if (!simple_object_internal_read (sobj->descriptor,
  449. sobj->offset + nametab_offset,
  450. (unsigned char *) nametab, nametab_size,
  451. errmsg, err))
  452. {
  453. XDELETEVEC (nametab);
  454. XDELETEVEC (secdata);
  455. return 0;
  456. }
  457. /* Fetch the index. */
  458. simple_object_mach_o_section_info (omr->is_big_endian, is_32,
  459. secdata + index_index * sechdrsize,
  460. &index_offset, &index_size);
  461. index = XNEWVEC (unsigned char, index_size);
  462. if (!simple_object_internal_read (sobj->descriptor,
  463. sobj->offset + index_offset,
  464. index, index_size,
  465. errmsg, err))
  466. {
  467. XDELETEVEC (index);
  468. XDELETEVEC (nametab);
  469. XDELETEVEC (secdata);
  470. return 0;
  471. }
  472. /* The index contains 4 unsigned ints per sub-section:
  473. sub-section offset/length, sub-section name/length.
  474. We fix this for both 32 and 64 bit mach-o for now, since
  475. other fields limit the maximum size of an object to 4G. */
  476. n_wrapped_sects = index_size / 16;
  477. /* Get the parameters for the wrapper too. */
  478. simple_object_mach_o_section_info (omr->is_big_endian, is_32,
  479. secdata + sections_index * sechdrsize,
  480. &wrapper_sect_offset,
  481. &wrapper_sect_size);
  482. }
  483. else
  484. {
  485. index = NULL;
  486. index_size = 0;
  487. nametab = NULL;
  488. nametab_size = 0;
  489. n_wrapped_sects = 0;
  490. }
  491. /* If we have a long names section, fetch it. */
  492. if ((gnu_sections_found & SOMO_LONGN_PRESENT) != 0)
  493. {
  494. off_t strtab_offset;
  495. simple_object_mach_o_section_info (omr->is_big_endian, is_32,
  496. secdata + strtab_index * sechdrsize,
  497. &strtab_offset, &strtab_size);
  498. strtab = XNEWVEC (char, strtab_size);
  499. if (!simple_object_internal_read (sobj->descriptor,
  500. sobj->offset + strtab_offset,
  501. (unsigned char *) strtab, strtab_size,
  502. errmsg, err))
  503. {
  504. XDELETEVEC (strtab);
  505. XDELETEVEC (index);
  506. XDELETEVEC (nametab);
  507. XDELETEVEC (secdata);
  508. return 0;
  509. }
  510. }
  511. else
  512. {
  513. strtab = NULL;
  514. strtab_size = 0;
  515. strtab_index = nsects;
  516. }
  517. /* Process the sections. */
  518. for (i = 0; i < nsects; ++i)
  519. {
  520. const unsigned char *sechdr;
  521. char namebuf[MACH_O_NAME_LEN * 2 + 2];
  522. char *name;
  523. off_t secoffset;
  524. size_t secsize;
  525. int l;
  526. sechdr = secdata + i * sechdrsize;
  527. /* We've already processed the long section names. */
  528. if ((gnu_sections_found & SOMO_LONGN_PRESENT) != 0
  529. && i == strtab_index)
  530. continue;
  531. /* We only act on the segment named. */
  532. if (strcmp ((char *) sechdr + segname_offset, omr->segment_name) != 0)
  533. continue;
  534. /* Process sections associated with the wrapper. */
  535. if ((gnu_sections_found & SOMO_WRAPPING) != 0)
  536. {
  537. if (i == nametab_index || i == index_index)
  538. continue;
  539. if (i == sections_index)
  540. {
  541. unsigned int j;
  542. for (j = 0; j < n_wrapped_sects; ++j)
  543. {
  544. unsigned int subsect_offset, subsect_length, name_offset;
  545. subsect_offset = (*fetch_32) (index + 16 * j);
  546. subsect_length = (*fetch_32) (index + 16 * j + 4);
  547. name_offset = (*fetch_32) (index + 16 * j + 8);
  548. /* We don't need the name_length yet. */
  549. secoffset = wrapper_sect_offset + subsect_offset;
  550. secsize = subsect_length;
  551. name = nametab + name_offset;
  552. if (!(*pfn) (data, name, secoffset, secsize))
  553. {
  554. *errmsg = NULL;
  555. *err = 0;
  556. XDELETEVEC (index);
  557. XDELETEVEC (nametab);
  558. XDELETEVEC (strtab);
  559. XDELETEVEC (secdata);
  560. return 0;
  561. }
  562. }
  563. continue;
  564. }
  565. }
  566. if ((gnu_sections_found & SOMO_LONGN_PRESENT) != 0)
  567. {
  568. memcpy (namebuf, sechdr + sectname_offset, MACH_O_NAME_LEN);
  569. namebuf[MACH_O_NAME_LEN] = '\0';
  570. name = &namebuf[0];
  571. if (strtab != NULL && name[0] == '_' && name[1] == '_')
  572. {
  573. unsigned long stringoffset;
  574. if (sscanf (name + 2, "%08lX", &stringoffset) == 1)
  575. {
  576. if (stringoffset >= strtab_size)
  577. {
  578. *errmsg = "section name offset out of range";
  579. *err = 0;
  580. XDELETEVEC (index);
  581. XDELETEVEC (nametab);
  582. XDELETEVEC (strtab);
  583. XDELETEVEC (secdata);
  584. return 0;
  585. }
  586. name = strtab + stringoffset;
  587. }
  588. }
  589. }
  590. else
  591. {
  592. /* Otherwise, make a name like __segment,__section as per the
  593. convention in mach-o asm. */
  594. name = &namebuf[0];
  595. memcpy (namebuf, (char *) sechdr + segname_offset, MACH_O_NAME_LEN);
  596. namebuf[MACH_O_NAME_LEN] = '\0';
  597. l = strlen (namebuf);
  598. namebuf[l] = ',';
  599. memcpy (namebuf + l + 1, (char *) sechdr + sectname_offset,
  600. MACH_O_NAME_LEN);
  601. namebuf[l + 1 + MACH_O_NAME_LEN] = '\0';
  602. }
  603. simple_object_mach_o_section_info (omr->is_big_endian, is_32, sechdr,
  604. &secoffset, &secsize);
  605. if (!(*pfn) (data, name, secoffset, secsize))
  606. {
  607. *errmsg = NULL;
  608. *err = 0;
  609. XDELETEVEC (index);
  610. XDELETEVEC (nametab);
  611. XDELETEVEC (strtab);
  612. XDELETEVEC (secdata);
  613. return 0;
  614. }
  615. }
  616. XDELETEVEC (index);
  617. XDELETEVEC (nametab);
  618. XDELETEVEC (strtab);
  619. XDELETEVEC (secdata);
  620. return 1;
  621. }
  622. /* Find all sections in a Mach-O file. */
  623. static const char *
  624. simple_object_mach_o_find_sections (simple_object_read *sobj,
  625. int (*pfn) (void *, const char *,
  626. off_t offset, off_t length),
  627. void *data,
  628. int *err)
  629. {
  630. struct simple_object_mach_o_read *omr =
  631. (struct simple_object_mach_o_read *) sobj->data;
  632. off_t offset;
  633. size_t seghdrsize;
  634. unsigned int (*fetch_32) (const unsigned char *);
  635. const char *errmsg;
  636. unsigned int i;
  637. if (omr->magic == MACH_O_MH_MAGIC)
  638. {
  639. offset = sizeof (struct mach_o_header_32);
  640. seghdrsize = sizeof (struct mach_o_segment_command_32);
  641. }
  642. else
  643. {
  644. offset = sizeof (struct mach_o_header_64);
  645. seghdrsize = sizeof (struct mach_o_segment_command_64);
  646. }
  647. fetch_32 = (omr->is_big_endian
  648. ? simple_object_fetch_big_32
  649. : simple_object_fetch_little_32);
  650. for (i = 0; i < omr->ncmds; ++i)
  651. {
  652. unsigned char loadbuf[sizeof (struct mach_o_load_command)];
  653. unsigned int cmd;
  654. unsigned int cmdsize;
  655. if (!simple_object_internal_read (sobj->descriptor,
  656. sobj->offset + offset,
  657. loadbuf,
  658. sizeof (struct mach_o_load_command),
  659. &errmsg, err))
  660. return errmsg;
  661. cmd = (*fetch_32) (loadbuf + offsetof (struct mach_o_load_command, cmd));
  662. cmdsize = (*fetch_32) (loadbuf
  663. + offsetof (struct mach_o_load_command, cmdsize));
  664. if (cmd == MACH_O_LC_SEGMENT || cmd == MACH_O_LC_SEGMENT_64)
  665. {
  666. unsigned char segbuf[sizeof (struct mach_o_segment_command_64)];
  667. int r;
  668. if (!simple_object_internal_read (sobj->descriptor,
  669. sobj->offset + offset,
  670. segbuf, seghdrsize, &errmsg, err))
  671. return errmsg;
  672. r = simple_object_mach_o_segment (sobj, offset, segbuf, pfn,
  673. data, &errmsg, err);
  674. if (!r)
  675. return errmsg;
  676. }
  677. offset += cmdsize;
  678. }
  679. return NULL;
  680. }
  681. /* Fetch the attributes for an simple_object_read. */
  682. static void *
  683. simple_object_mach_o_fetch_attributes (simple_object_read *sobj,
  684. const char **errmsg ATTRIBUTE_UNUSED,
  685. int *err ATTRIBUTE_UNUSED)
  686. {
  687. struct simple_object_mach_o_read *omr =
  688. (struct simple_object_mach_o_read *) sobj->data;
  689. struct simple_object_mach_o_attributes *ret;
  690. ret = XNEW (struct simple_object_mach_o_attributes);
  691. ret->magic = omr->magic;
  692. ret->is_big_endian = omr->is_big_endian;
  693. ret->cputype = omr->cputype;
  694. ret->cpusubtype = omr->cpusubtype;
  695. ret->flags = omr->flags;
  696. ret->reserved = omr->reserved;
  697. return ret;
  698. }
  699. /* Release the private data for an simple_object_read. */
  700. static void
  701. simple_object_mach_o_release_read (void *data)
  702. {
  703. struct simple_object_mach_o_read *omr =
  704. (struct simple_object_mach_o_read *) data;
  705. free (omr->segment_name);
  706. XDELETE (omr);
  707. }
  708. /* Compare two attributes structures. */
  709. static const char *
  710. simple_object_mach_o_attributes_merge (void *todata, void *fromdata, int *err)
  711. {
  712. struct simple_object_mach_o_attributes *to =
  713. (struct simple_object_mach_o_attributes *) todata;
  714. struct simple_object_mach_o_attributes *from =
  715. (struct simple_object_mach_o_attributes *) fromdata;
  716. if (to->magic != from->magic
  717. || to->is_big_endian != from->is_big_endian
  718. || to->cputype != from->cputype)
  719. {
  720. *err = 0;
  721. return "Mach-O object format mismatch";
  722. }
  723. return NULL;
  724. }
  725. /* Release the private data for an attributes structure. */
  726. static void
  727. simple_object_mach_o_release_attributes (void *data)
  728. {
  729. XDELETE (data);
  730. }
  731. /* Prepare to write out a file. */
  732. static void *
  733. simple_object_mach_o_start_write (void *attributes_data,
  734. const char **errmsg ATTRIBUTE_UNUSED,
  735. int *err ATTRIBUTE_UNUSED)
  736. {
  737. struct simple_object_mach_o_attributes *attrs =
  738. (struct simple_object_mach_o_attributes *) attributes_data;
  739. struct simple_object_mach_o_attributes *ret;
  740. /* We're just going to record the attributes, but we need to make a
  741. copy because the user may delete them. */
  742. ret = XNEW (struct simple_object_mach_o_attributes);
  743. *ret = *attrs;
  744. return ret;
  745. }
  746. /* Write out the header of a Mach-O file. */
  747. static int
  748. simple_object_mach_o_write_header (simple_object_write *sobj, int descriptor,
  749. size_t nsects, const char **errmsg,
  750. int *err)
  751. {
  752. struct simple_object_mach_o_attributes *attrs =
  753. (struct simple_object_mach_o_attributes *) sobj->data;
  754. void (*set_32) (unsigned char *, unsigned int);
  755. unsigned char hdrbuf[sizeof (struct mach_o_header_64)];
  756. unsigned char *hdr;
  757. size_t wrsize;
  758. set_32 = (attrs->is_big_endian
  759. ? simple_object_set_big_32
  760. : simple_object_set_little_32);
  761. memset (hdrbuf, 0, sizeof hdrbuf);
  762. /* The 32-bit and 64-bit headers start out the same. */
  763. hdr = &hdrbuf[0];
  764. set_32 (hdr + offsetof (struct mach_o_header_32, magic), attrs->magic);
  765. set_32 (hdr + offsetof (struct mach_o_header_32, cputype), attrs->cputype);
  766. set_32 (hdr + offsetof (struct mach_o_header_32, cpusubtype),
  767. attrs->cpusubtype);
  768. set_32 (hdr + offsetof (struct mach_o_header_32, filetype), MACH_O_MH_OBJECT);
  769. set_32 (hdr + offsetof (struct mach_o_header_32, ncmds), 1);
  770. set_32 (hdr + offsetof (struct mach_o_header_32, flags), attrs->flags);
  771. if (attrs->magic == MACH_O_MH_MAGIC)
  772. {
  773. wrsize = sizeof (struct mach_o_header_32);
  774. set_32 (hdr + offsetof (struct mach_o_header_32, sizeofcmds),
  775. (sizeof (struct mach_o_segment_command_32)
  776. + nsects * sizeof (struct mach_o_section_32)));
  777. }
  778. else
  779. {
  780. set_32 (hdr + offsetof (struct mach_o_header_64, sizeofcmds),
  781. (sizeof (struct mach_o_segment_command_64)
  782. + nsects * sizeof (struct mach_o_section_64)));
  783. set_32 (hdr + offsetof (struct mach_o_header_64, reserved),
  784. attrs->reserved);
  785. wrsize = sizeof (struct mach_o_header_64);
  786. }
  787. return simple_object_internal_write (descriptor, 0, hdrbuf, wrsize,
  788. errmsg, err);
  789. }
  790. /* Write a Mach-O section header. */
  791. static int
  792. simple_object_mach_o_write_section_header (simple_object_write *sobj,
  793. int descriptor,
  794. size_t sechdr_offset,
  795. const char *name, const char *segn,
  796. size_t secaddr, size_t secsize,
  797. size_t offset, unsigned int align,
  798. const char **errmsg, int *err)
  799. {
  800. struct simple_object_mach_o_attributes *attrs =
  801. (struct simple_object_mach_o_attributes *) sobj->data;
  802. void (*set_32) (unsigned char *, unsigned int);
  803. unsigned char hdrbuf[sizeof (struct mach_o_section_64)];
  804. unsigned char *hdr;
  805. size_t sechdrsize;
  806. set_32 = (attrs->is_big_endian
  807. ? simple_object_set_big_32
  808. : simple_object_set_little_32);
  809. memset (hdrbuf, 0, sizeof hdrbuf);
  810. hdr = &hdrbuf[0];
  811. if (attrs->magic == MACH_O_MH_MAGIC)
  812. {
  813. strncpy ((char *) hdr + offsetof (struct mach_o_section_32, sectname),
  814. name, MACH_O_NAME_LEN);
  815. strncpy ((char *) hdr + offsetof (struct mach_o_section_32, segname),
  816. segn, MACH_O_NAME_LEN);
  817. set_32 (hdr + offsetof (struct mach_o_section_32, addr), secaddr);
  818. set_32 (hdr + offsetof (struct mach_o_section_32, size), secsize);
  819. set_32 (hdr + offsetof (struct mach_o_section_32, offset), offset);
  820. set_32 (hdr + offsetof (struct mach_o_section_32, align), align);
  821. /* reloff left as zero. */
  822. /* nreloc left as zero. */
  823. set_32 (hdr + offsetof (struct mach_o_section_32, flags),
  824. MACH_O_S_ATTR_DEBUG);
  825. /* reserved1 left as zero. */
  826. /* reserved2 left as zero. */
  827. sechdrsize = sizeof (struct mach_o_section_32);
  828. }
  829. else
  830. {
  831. #ifdef UNSIGNED_64BIT_TYPE
  832. void (*set_64) (unsigned char *, ulong_type);
  833. set_64 = (attrs->is_big_endian
  834. ? simple_object_set_big_64
  835. : simple_object_set_little_64);
  836. strncpy ((char *) hdr + offsetof (struct mach_o_section_64, sectname),
  837. name, MACH_O_NAME_LEN);
  838. strncpy ((char *) hdr + offsetof (struct mach_o_section_64, segname),
  839. segn, MACH_O_NAME_LEN);
  840. set_64 (hdr + offsetof (struct mach_o_section_64, addr), secaddr);
  841. set_64 (hdr + offsetof (struct mach_o_section_64, size), secsize);
  842. set_32 (hdr + offsetof (struct mach_o_section_64, offset), offset);
  843. set_32 (hdr + offsetof (struct mach_o_section_64, align), align);
  844. /* reloff left as zero. */
  845. /* nreloc left as zero. */
  846. set_32 (hdr + offsetof (struct mach_o_section_64, flags),
  847. MACH_O_S_ATTR_DEBUG);
  848. /* reserved1 left as zero. */
  849. /* reserved2 left as zero. */
  850. /* reserved3 left as zero. */
  851. #endif
  852. sechdrsize = sizeof (struct mach_o_section_64);
  853. }
  854. return simple_object_internal_write (descriptor, sechdr_offset, hdr,
  855. sechdrsize, errmsg, err);
  856. }
  857. /* Write out the single (anonymous) segment containing the sections of a Mach-O
  858. Object file.
  859. As a GNU extension to mach-o, when the caller specifies a segment name in
  860. sobj->segment_name, all the sections passed will be output under a single
  861. mach-o section header. The caller's sections are indexed within this
  862. 'wrapper' section by a table stored in a second mach-o section. Finally,
  863. arbitrary length section names are permitted by the extension and these are
  864. stored in a table in a third mach-o section.
  865. Note that this is only likely to make any sense for the __GNU_LTO segment
  866. at present.
  867. If the wrapper extension is not in force, we assume that the section name
  868. is in the form __SEGMENT_NAME,__section_name as per Mach-O asm. */
  869. static int
  870. simple_object_mach_o_write_segment (simple_object_write *sobj, int descriptor,
  871. size_t *nsects, const char **errmsg,
  872. int *err)
  873. {
  874. struct simple_object_mach_o_attributes *attrs =
  875. (struct simple_object_mach_o_attributes *) sobj->data;
  876. void (*set_32) (unsigned char *, unsigned int);
  877. size_t hdrsize;
  878. size_t seghdrsize;
  879. size_t sechdrsize;
  880. size_t cmdsize;
  881. size_t offset;
  882. size_t sechdr_offset;
  883. size_t secaddr;
  884. unsigned int name_offset;
  885. simple_object_write_section *section;
  886. unsigned char hdrbuf[sizeof (struct mach_o_segment_command_64)];
  887. unsigned char *hdr;
  888. size_t nsects_in;
  889. unsigned int *index;
  890. char *snames;
  891. unsigned int sect;
  892. set_32 = (attrs->is_big_endian
  893. ? simple_object_set_big_32
  894. : simple_object_set_little_32);
  895. /* Write out the sections first. */
  896. if (attrs->magic == MACH_O_MH_MAGIC)
  897. {
  898. hdrsize = sizeof (struct mach_o_header_32);
  899. seghdrsize = sizeof (struct mach_o_segment_command_32);
  900. sechdrsize = sizeof (struct mach_o_section_32);
  901. }
  902. else
  903. {
  904. hdrsize = sizeof (struct mach_o_header_64);
  905. seghdrsize = sizeof (struct mach_o_segment_command_64);
  906. sechdrsize = sizeof (struct mach_o_section_64);
  907. }
  908. name_offset = 0;
  909. *nsects = nsects_in = 0;
  910. /* Count the number of sections we start with. */
  911. for (section = sobj->sections; section != NULL; section = section->next)
  912. nsects_in++;
  913. if (sobj->segment_name != NULL)
  914. {
  915. /* We will only write 3 sections: wrapped data, index and names. */
  916. *nsects = 3;
  917. /* The index has four entries per wrapped section:
  918. Section Offset, length, Name offset, length.
  919. Where the offsets are based at the start of the wrapper and name
  920. sections respectively.
  921. The values are stored as 32 bit int for both 32 and 64 bit mach-o
  922. since the size of a mach-o MH_OBJECT cannot exceed 4G owing to
  923. other constraints. */
  924. index = XNEWVEC (unsigned int, nsects_in * 4);
  925. /* We now need to figure out the size of the names section. This just
  926. stores the names as null-terminated c strings, packed without any
  927. alignment padding. */
  928. for (section = sobj->sections, sect = 0; section != NULL;
  929. section = section->next, sect++)
  930. {
  931. index[sect*4+2] = name_offset;
  932. index[sect*4+3] = strlen (section->name) + 1;
  933. name_offset += strlen (section->name) + 1;
  934. }
  935. snames = XNEWVEC (char, name_offset);
  936. }
  937. else
  938. {
  939. *nsects = nsects_in;
  940. index = NULL;
  941. snames = NULL;
  942. }
  943. sechdr_offset = hdrsize + seghdrsize;
  944. cmdsize = seghdrsize + *nsects * sechdrsize;
  945. offset = hdrsize + cmdsize;
  946. secaddr = 0;
  947. for (section = sobj->sections, sect = 0;
  948. section != NULL; section = section->next, sect++)
  949. {
  950. size_t mask;
  951. size_t new_offset;
  952. size_t secsize;
  953. struct simple_object_write_section_buffer *buffer;
  954. mask = (1U << section->align) - 1;
  955. new_offset = offset + mask;
  956. new_offset &= ~ mask;
  957. while (new_offset > offset)
  958. {
  959. unsigned char zeroes[16];
  960. size_t write;
  961. memset (zeroes, 0, sizeof zeroes);
  962. write = new_offset - offset;
  963. if (write > sizeof zeroes)
  964. write = sizeof zeroes;
  965. if (!simple_object_internal_write (descriptor, offset, zeroes, write,
  966. errmsg, err))
  967. return 0;
  968. offset += write;
  969. }
  970. secsize = 0;
  971. for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
  972. {
  973. if (!simple_object_internal_write (descriptor, offset + secsize,
  974. ((const unsigned char *)
  975. buffer->buffer),
  976. buffer->size, errmsg, err))
  977. return 0;
  978. secsize += buffer->size;
  979. }
  980. if (sobj->segment_name != NULL)
  981. {
  982. index[sect*4+0] = (unsigned int) offset;
  983. index[sect*4+1] = secsize;
  984. /* Stash the section name in our table. */
  985. memcpy (snames + index[sect * 4 + 2], section->name,
  986. index[sect * 4 + 3]);
  987. }
  988. else
  989. {
  990. char namebuf[MACH_O_NAME_LEN + 1];
  991. char segnbuf[MACH_O_NAME_LEN + 1];
  992. char *comma;
  993. /* Try to extract segment,section from the input name. */
  994. memset (namebuf, 0, sizeof namebuf);
  995. memset (segnbuf, 0, sizeof segnbuf);
  996. comma = strchr (section->name, ',');
  997. if (comma != NULL)
  998. {
  999. int len = comma - section->name;
  1000. len = len > MACH_O_NAME_LEN ? MACH_O_NAME_LEN : len;
  1001. strncpy (namebuf, section->name, len);
  1002. strncpy (segnbuf, comma + 1, MACH_O_NAME_LEN);
  1003. }
  1004. else /* just try to copy the name, leave segment blank. */
  1005. strncpy (namebuf, section->name, MACH_O_NAME_LEN);
  1006. if (!simple_object_mach_o_write_section_header (sobj, descriptor,
  1007. sechdr_offset,
  1008. namebuf, segnbuf,
  1009. secaddr, secsize,
  1010. offset,
  1011. section->align,
  1012. errmsg, err))
  1013. return 0;
  1014. sechdr_offset += sechdrsize;
  1015. }
  1016. offset += secsize;
  1017. secaddr += secsize;
  1018. }
  1019. if (sobj->segment_name != NULL)
  1020. {
  1021. size_t secsize;
  1022. unsigned int i;
  1023. /* Write the section header for the wrapper. */
  1024. /* Account for any initial aligment - which becomes the alignment for this
  1025. created section. */
  1026. secsize = (offset - index[0]);
  1027. if (!simple_object_mach_o_write_section_header (sobj, descriptor,
  1028. sechdr_offset,
  1029. GNU_WRAPPER_SECTS,
  1030. sobj->segment_name,
  1031. 0 /*secaddr*/,
  1032. secsize, index[0],
  1033. sobj->sections->align,
  1034. errmsg, err))
  1035. return 0;
  1036. /* Subtract the wrapper section start from the begining of each sub
  1037. section. */
  1038. for (i = 1; i < nsects_in; ++i)
  1039. index[4 * i] -= index[0];
  1040. index[0] = 0;
  1041. /* Swap the indices, if required. */
  1042. for (i = 0; i < (nsects_in * 4); ++i)
  1043. set_32 ((unsigned char *) &index[i], index[i]);
  1044. sechdr_offset += sechdrsize;
  1045. /* Write out the section names.
  1046. ... the header ...
  1047. name_offset contains the length of the section. It is not aligned. */
  1048. if (!simple_object_mach_o_write_section_header (sobj, descriptor,
  1049. sechdr_offset,
  1050. GNU_WRAPPER_NAMES,
  1051. sobj->segment_name,
  1052. 0 /*secaddr*/,
  1053. name_offset,
  1054. offset,
  1055. 0, errmsg, err))
  1056. return 0;
  1057. /* ... and the content.. */
  1058. if (!simple_object_internal_write (descriptor, offset,
  1059. (const unsigned char *) snames,
  1060. name_offset, errmsg, err))
  1061. return 0;
  1062. sechdr_offset += sechdrsize;
  1063. secaddr += name_offset;
  1064. offset += name_offset;
  1065. /* Now do the index, we'll align this to 4 bytes although the read code
  1066. will handle unaligned. */
  1067. offset += 3;
  1068. offset &= ~0x03;
  1069. if (!simple_object_mach_o_write_section_header (sobj, descriptor,
  1070. sechdr_offset,
  1071. GNU_WRAPPER_INDEX,
  1072. sobj->segment_name,
  1073. 0 /*secaddr*/,
  1074. nsects_in * 16,
  1075. offset,
  1076. 2, errmsg, err))
  1077. return 0;
  1078. /* ... and the content.. */
  1079. if (!simple_object_internal_write (descriptor, offset,
  1080. (const unsigned char *) index,
  1081. nsects_in*16, errmsg, err))
  1082. return 0;
  1083. XDELETEVEC (index);
  1084. XDELETEVEC (snames);
  1085. }
  1086. /* Write out the segment header. */
  1087. memset (hdrbuf, 0, sizeof hdrbuf);
  1088. hdr = &hdrbuf[0];
  1089. if (attrs->magic == MACH_O_MH_MAGIC)
  1090. {
  1091. set_32 (hdr + offsetof (struct mach_o_segment_command_32, cmd),
  1092. MACH_O_LC_SEGMENT);
  1093. set_32 (hdr + offsetof (struct mach_o_segment_command_32, cmdsize),
  1094. cmdsize);
  1095. /* MH_OBJECTS have a single, anonymous, segment - so the segment name
  1096. is left empty. */
  1097. /* vmaddr left as zero. */
  1098. /* vmsize left as zero. */
  1099. set_32 (hdr + offsetof (struct mach_o_segment_command_32, fileoff),
  1100. hdrsize + cmdsize);
  1101. set_32 (hdr + offsetof (struct mach_o_segment_command_32, filesize),
  1102. offset - (hdrsize + cmdsize));
  1103. /* maxprot left as zero. */
  1104. /* initprot left as zero. */
  1105. set_32 (hdr + offsetof (struct mach_o_segment_command_32, nsects),
  1106. *nsects);
  1107. /* flags left as zero. */
  1108. }
  1109. else
  1110. {
  1111. #ifdef UNSIGNED_64BIT_TYPE
  1112. void (*set_64) (unsigned char *, ulong_type);
  1113. set_64 = (attrs->is_big_endian
  1114. ? simple_object_set_big_64
  1115. : simple_object_set_little_64);
  1116. set_32 (hdr + offsetof (struct mach_o_segment_command_64, cmd),
  1117. MACH_O_LC_SEGMENT);
  1118. set_32 (hdr + offsetof (struct mach_o_segment_command_64, cmdsize),
  1119. cmdsize);
  1120. /* MH_OBJECTS have a single, anonymous, segment - so the segment name
  1121. is left empty. */
  1122. /* vmaddr left as zero. */
  1123. /* vmsize left as zero. */
  1124. set_64 (hdr + offsetof (struct mach_o_segment_command_64, fileoff),
  1125. hdrsize + cmdsize);
  1126. set_64 (hdr + offsetof (struct mach_o_segment_command_64, filesize),
  1127. offset - (hdrsize + cmdsize));
  1128. /* maxprot left as zero. */
  1129. /* initprot left as zero. */
  1130. set_32 (hdr + offsetof (struct mach_o_segment_command_64, nsects),
  1131. *nsects);
  1132. /* flags left as zero. */
  1133. #endif
  1134. }
  1135. return simple_object_internal_write (descriptor, hdrsize, hdr, seghdrsize,
  1136. errmsg, err);
  1137. }
  1138. /* Write out a complete Mach-O file. */
  1139. static const char *
  1140. simple_object_mach_o_write_to_file (simple_object_write *sobj, int descriptor,
  1141. int *err)
  1142. {
  1143. size_t nsects = 0;
  1144. const char *errmsg;
  1145. if (!simple_object_mach_o_write_segment (sobj, descriptor, &nsects,
  1146. &errmsg, err))
  1147. return errmsg;
  1148. if (!simple_object_mach_o_write_header (sobj, descriptor, nsects,
  1149. &errmsg, err))
  1150. return errmsg;
  1151. return NULL;
  1152. }
  1153. /* Release the private data for an simple_object_write structure. */
  1154. static void
  1155. simple_object_mach_o_release_write (void *data)
  1156. {
  1157. XDELETE (data);
  1158. }
  1159. /* The Mach-O functions. */
  1160. const struct simple_object_functions simple_object_mach_o_functions =
  1161. {
  1162. simple_object_mach_o_match,
  1163. simple_object_mach_o_find_sections,
  1164. simple_object_mach_o_fetch_attributes,
  1165. simple_object_mach_o_release_read,
  1166. simple_object_mach_o_attributes_merge,
  1167. simple_object_mach_o_release_attributes,
  1168. simple_object_mach_o_start_write,
  1169. simple_object_mach_o_write_to_file,
  1170. simple_object_mach_o_release_write,
  1171. NULL
  1172. };