simple-object-mach-o.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /* simple-object-mach-o.c -- routines to manipulate Mach-O object files.
  2. Copyright 2010, 2011, 2013 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. sechdr_offset += sechdrsize;
  1042. /* Write out the section names.
  1043. ... the header ...
  1044. name_offset contains the length of the section. It is not aligned. */
  1045. if (!simple_object_mach_o_write_section_header (sobj, descriptor,
  1046. sechdr_offset,
  1047. GNU_WRAPPER_NAMES,
  1048. sobj->segment_name,
  1049. 0 /*secaddr*/,
  1050. name_offset,
  1051. offset,
  1052. 0, errmsg, err))
  1053. return 0;
  1054. /* ... and the content.. */
  1055. if (!simple_object_internal_write (descriptor, offset,
  1056. (const unsigned char *) snames,
  1057. name_offset, errmsg, err))
  1058. return 0;
  1059. sechdr_offset += sechdrsize;
  1060. secaddr += name_offset;
  1061. offset += name_offset;
  1062. /* Now do the index, we'll align this to 4 bytes although the read code
  1063. will handle unaligned. */
  1064. offset += 3;
  1065. offset &= ~0x03;
  1066. if (!simple_object_mach_o_write_section_header (sobj, descriptor,
  1067. sechdr_offset,
  1068. GNU_WRAPPER_INDEX,
  1069. sobj->segment_name,
  1070. 0 /*secaddr*/,
  1071. nsects_in * 16,
  1072. offset,
  1073. 2, errmsg, err))
  1074. return 0;
  1075. /* ... and the content.. */
  1076. if (!simple_object_internal_write (descriptor, offset,
  1077. (const unsigned char *) index,
  1078. nsects_in*16, errmsg, err))
  1079. return 0;
  1080. XDELETEVEC (index);
  1081. XDELETEVEC (snames);
  1082. }
  1083. /* Write out the segment header. */
  1084. memset (hdrbuf, 0, sizeof hdrbuf);
  1085. hdr = &hdrbuf[0];
  1086. if (attrs->magic == MACH_O_MH_MAGIC)
  1087. {
  1088. set_32 (hdr + offsetof (struct mach_o_segment_command_32, cmd),
  1089. MACH_O_LC_SEGMENT);
  1090. set_32 (hdr + offsetof (struct mach_o_segment_command_32, cmdsize),
  1091. cmdsize);
  1092. /* MH_OBJECTS have a single, anonymous, segment - so the segment name
  1093. is left empty. */
  1094. /* vmaddr left as zero. */
  1095. /* vmsize left as zero. */
  1096. set_32 (hdr + offsetof (struct mach_o_segment_command_32, fileoff),
  1097. hdrsize + cmdsize);
  1098. set_32 (hdr + offsetof (struct mach_o_segment_command_32, filesize),
  1099. offset - (hdrsize + cmdsize));
  1100. /* maxprot left as zero. */
  1101. /* initprot left as zero. */
  1102. set_32 (hdr + offsetof (struct mach_o_segment_command_32, nsects),
  1103. *nsects);
  1104. /* flags left as zero. */
  1105. }
  1106. else
  1107. {
  1108. #ifdef UNSIGNED_64BIT_TYPE
  1109. void (*set_64) (unsigned char *, ulong_type);
  1110. set_64 = (attrs->is_big_endian
  1111. ? simple_object_set_big_64
  1112. : simple_object_set_little_64);
  1113. set_32 (hdr + offsetof (struct mach_o_segment_command_64, cmd),
  1114. MACH_O_LC_SEGMENT);
  1115. set_32 (hdr + offsetof (struct mach_o_segment_command_64, cmdsize),
  1116. cmdsize);
  1117. /* MH_OBJECTS have a single, anonymous, segment - so the segment name
  1118. is left empty. */
  1119. /* vmaddr left as zero. */
  1120. /* vmsize left as zero. */
  1121. set_64 (hdr + offsetof (struct mach_o_segment_command_64, fileoff),
  1122. hdrsize + cmdsize);
  1123. set_64 (hdr + offsetof (struct mach_o_segment_command_64, filesize),
  1124. offset - (hdrsize + cmdsize));
  1125. /* maxprot left as zero. */
  1126. /* initprot left as zero. */
  1127. set_32 (hdr + offsetof (struct mach_o_segment_command_64, nsects),
  1128. *nsects);
  1129. /* flags left as zero. */
  1130. #endif
  1131. }
  1132. return simple_object_internal_write (descriptor, hdrsize, hdr, seghdrsize,
  1133. errmsg, err);
  1134. }
  1135. /* Write out a complete Mach-O file. */
  1136. static const char *
  1137. simple_object_mach_o_write_to_file (simple_object_write *sobj, int descriptor,
  1138. int *err)
  1139. {
  1140. size_t nsects = 0;
  1141. const char *errmsg;
  1142. if (!simple_object_mach_o_write_segment (sobj, descriptor, &nsects,
  1143. &errmsg, err))
  1144. return errmsg;
  1145. if (!simple_object_mach_o_write_header (sobj, descriptor, nsects,
  1146. &errmsg, err))
  1147. return errmsg;
  1148. return NULL;
  1149. }
  1150. /* Release the private data for an simple_object_write structure. */
  1151. static void
  1152. simple_object_mach_o_release_write (void *data)
  1153. {
  1154. XDELETE (data);
  1155. }
  1156. /* The Mach-O functions. */
  1157. const struct simple_object_functions simple_object_mach_o_functions =
  1158. {
  1159. simple_object_mach_o_match,
  1160. simple_object_mach_o_find_sections,
  1161. simple_object_mach_o_fetch_attributes,
  1162. simple_object_mach_o_release_read,
  1163. simple_object_mach_o_attributes_merge,
  1164. simple_object_mach_o_release_attributes,
  1165. simple_object_mach_o_start_write,
  1166. simple_object_mach_o_write_to_file,
  1167. simple_object_mach_o_release_write
  1168. };