2 * Mach-O file handling for TCC
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* In order to make life easy for us we are generating Mach-O files which
21 don't make use of some modern features, but which aren't entirely classic
22 either in that they do use some modern features. We're also only
23 generating 64bit Mach-O files, and only native endian at that.
25 In particular we're generating executables that don't make use of
26 DYLD_INFO for dynamic linking info, as that requires us building a
27 trie of exported names. We're simply using classic symbol tables which
28 are still supported by modern dyld.
30 But we do use LC_MAIN, which is a "modern" feature in order to not have
31 to setup our own crt code. We're not using lazy linking, so even function
32 calls are resolved at startup. */
34 #if !defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64
35 #error Platform not supported
39 #define dprintf if (DEBUG_MACHO) printf
41 #define MH_EXECUTE (0x2)
42 #define MH_DYLDLINK (0x4)
43 #define MH_DYLIB (0x6)
44 #define MH_PIE (0x200000)
46 #define CPU_SUBTYPE_LIB64 (0x80000000)
47 #define CPU_SUBTYPE_X86_ALL (3)
48 #define CPU_SUBTYPE_ARM64_ALL (0)
50 #define CPU_ARCH_ABI64 (0x01000000)
52 #define CPU_TYPE_X86 (7)
53 #define CPU_TYPE_X86_64 (CPU_TYPE_X86 | CPU_ARCH_ABI64)
54 #define CPU_TYPE_ARM (12)
55 #define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64)
58 uint32_t magic
; /* FAT_MAGIC or FAT_MAGIC_64 */
59 uint32_t nfat_arch
; /* number of structs that follow */
63 int cputype
; /* cpu specifier (int) */
64 int cpusubtype
; /* machine specifier (int) */
65 uint32_t offset
; /* file offset to this object file */
66 uint32_t size
; /* size of this object file */
67 uint32_t align
; /* alignment as a power of 2 */
70 #define FAT_MAGIC 0xcafebabe
71 #define FAT_CIGAM 0xbebafeca
72 #define FAT_MAGIC_64 0xcafebabf
73 #define FAT_CIGAM_64 0xbfbafeca
76 uint32_t magic
; /* mach magic number identifier */
77 int cputype
; /* cpu specifier */
78 int cpusubtype
; /* machine specifier */
79 uint32_t filetype
; /* type of file */
80 uint32_t ncmds
; /* number of load commands */
81 uint32_t sizeofcmds
; /* the size of all the load commands */
82 uint32_t flags
; /* flags */
85 struct mach_header_64
{
86 struct mach_header mh
;
87 uint32_t reserved
; /* reserved, pad to 64bit */
90 /* Constant for the magic field of the mach_header (32-bit architectures) */
91 #define MH_MAGIC 0xfeedface /* the mach magic number */
92 #define MH_CIGAM 0xcefaedfe /* NXSwapInt(MH_MAGIC) */
93 #define MH_MAGIC_64 0xfeedfacf /* the 64-bit mach magic number */
94 #define MH_CIGAM_64 0xcffaedfe /* NXSwapInt(MH_MAGIC_64) */
97 uint32_t cmd
; /* type of load command */
98 uint32_t cmdsize
; /* total size of command in bytes */
101 #define LC_REQ_DYLD 0x80000000
102 #define LC_SYMTAB 0x2
103 #define LC_DYSYMTAB 0xb
104 #define LC_LOAD_DYLIB 0xc
105 #define LC_ID_DYLIB 0xd
106 #define LC_LOAD_DYLINKER 0xe
107 #define LC_SEGMENT_64 0x19
108 #define LC_RPATH (0x1c | LC_REQ_DYLD)
109 #define LC_REEXPORT_DYLIB (0x1f | LC_REQ_DYLD)
110 #define LC_DYLD_INFO_ONLY (0x22|LC_REQ_DYLD)
111 #define LC_MAIN (0x28|LC_REQ_DYLD)
112 #define LC_SOURCE_VERSION 0x2A
113 #define LC_BUILD_VERSION 0x32
114 #define LC_DYLD_EXPORTS_TRIE (0x33 | LC_REQ_DYLD)
115 #define LC_DYLD_CHAINED_FIXUPS (0x34 | LC_REQ_DYLD)
117 #define SG_READ_ONLY 0x10 /* This segment is made read-only after fixups */
119 typedef int vm_prot_t
;
121 struct segment_command_64
{ /* for 64-bit architectures */
122 uint32_t cmd
; /* LC_SEGMENT_64 */
123 uint32_t cmdsize
; /* includes sizeof section_64 structs */
124 char segname
[16]; /* segment name */
125 uint64_t vmaddr
; /* memory address of this segment */
126 uint64_t vmsize
; /* memory size of this segment */
127 uint64_t fileoff
; /* file offset of this segment */
128 uint64_t filesize
; /* amount to map from the file */
129 vm_prot_t maxprot
; /* maximum VM protection */
130 vm_prot_t initprot
; /* initial VM protection */
131 uint32_t nsects
; /* number of sections in segment */
132 uint32_t flags
; /* flags */
135 struct section_64
{ /* for 64-bit architectures */
136 char sectname
[16]; /* name of this section */
137 char segname
[16]; /* segment this section goes in */
138 uint64_t addr
; /* memory address of this section */
139 uint64_t size
; /* size in bytes of this section */
140 uint32_t offset
; /* file offset of this section */
141 uint32_t align
; /* section alignment (power of 2) */
142 uint32_t reloff
; /* file offset of relocation entries */
143 uint32_t nreloc
; /* number of relocation entries */
144 uint32_t flags
; /* flags (section type and attributes)*/
145 uint32_t reserved1
; /* reserved (for offset or index) */
146 uint32_t reserved2
; /* reserved (for count or sizeof) */
147 uint32_t reserved3
; /* reserved */
151 DYLD_CHAINED_IMPORT
= 1,
154 struct dyld_chained_fixups_header
{
155 uint32_t fixups_version
; ///< 0
156 uint32_t starts_offset
; ///< Offset of dyld_chained_starts_in_image.
157 uint32_t imports_offset
; ///< Offset of imports table in chain_data.
158 uint32_t symbols_offset
; ///< Offset of symbol strings in chain_data.
159 uint32_t imports_count
; ///< Number of imported symbol names.
160 uint32_t imports_format
; ///< DYLD_CHAINED_IMPORT*
161 uint32_t symbols_format
; ///< 0 => uncompressed, 1 => zlib compressed
164 struct dyld_chained_starts_in_image
167 uint32_t seg_info_offset
[1]; // each entry is offset into this struct for that segment
168 // followed by pool of dyld_chain_starts_in_segment data
172 DYLD_CHAINED_PTR_64
= 2, // target is vmaddr
173 DYLD_CHAINED_PTR_64_OFFSET
= 6, // target is vm offset
177 DYLD_CHAINED_PTR_START_NONE
= 0xFFFF, // used in page_start[] to denote a page with no fixups
180 #define SEG_PAGE_SIZE 16384
182 struct dyld_chained_starts_in_segment
184 uint32_t size
; // size of this (amount kernel needs to copy)
185 uint16_t page_size
; // 0x1000 or 0x4000
186 uint16_t pointer_format
; // DYLD_CHAINED_PTR_*
187 uint64_t segment_offset
; // offset in memory to start of segment
188 uint32_t max_valid_pointer
; // for 32-bit OS, any value beyond this is not a pointer
189 uint16_t page_count
; // how many pages are in array
190 uint16_t page_start
[1]; // each entry is offset in each page of first element in chain
191 // or DYLD_CHAINED_PTR_START_NONE if no fixups on page
194 enum BindSpecialDylib
{
195 BIND_SPECIAL_DYLIB_FLAT_LOOKUP
= -2,
198 struct dyld_chained_import
200 uint32_t lib_ordinal
: 8,
205 struct dyld_chained_ptr_64_rebase
207 uint64_t target
: 36, // vmaddr, 64GB max image size
208 high8
: 8, // top 8 bits set to this after slide added
209 reserved
: 7, // all zeros
210 next
: 12, // 4-byte stride
214 struct dyld_chained_ptr_64_bind
216 uint64_t ordinal
: 24,
217 addend
: 8, // 0 thru 255
218 reserved
: 19, // all zeros
219 next
: 12, // 4-byte stride
223 #define S_REGULAR 0x0
224 #define S_ZEROFILL 0x1
225 #define S_NON_LAZY_SYMBOL_POINTERS 0x6
226 #define S_LAZY_SYMBOL_POINTERS 0x7
227 #define S_SYMBOL_STUBS 0x8
228 #define S_MOD_INIT_FUNC_POINTERS 0x9
229 #define S_MOD_TERM_FUNC_POINTERS 0xa
231 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000
232 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400
233 #define S_ATTR_DEBUG 0x02000000
236 typedef uint32_t lc_str
;
238 struct dylib_command
{
239 uint32_t cmd
; /* LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB,
241 uint32_t cmdsize
; /* includes pathname string */
242 lc_str name
; /* library's path name */
243 uint32_t timestamp
; /* library's build time stamp */
244 uint32_t current_version
; /* library's current version number */
245 uint32_t compatibility_version
; /* library's compatibility vers number*/
248 struct rpath_command
{
249 uint32_t cmd
; /* LC_RPATH */
250 uint32_t cmdsize
; /* includes string */
251 lc_str path
; /* path to add to run path */
254 struct dylinker_command
{
255 uint32_t cmd
; /* LC_ID_DYLINKER, LC_LOAD_DYLINKER or
256 LC_DYLD_ENVIRONMENT */
257 uint32_t cmdsize
; /* includes pathname string */
258 lc_str name
; /* dynamic linker's path name */
261 struct linkedit_data_command
{
262 uint32_t cmd
; /* LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO,
263 LC_FUNCTION_STARTS, LC_DATA_IN_CODE,
264 LC_DYLIB_CODE_SIGN_DRS,
265 LC_LINKER_OPTIMIZATION_HINT,
266 LC_DYLD_EXPORTS_TRIE, or
267 LC_DYLD_CHAINED_FIXUPS. */
268 uint32_t cmdsize
; /* sizeof(struct linkedit_data_command) */
269 uint32_t dataoff
; /* file offset of data in __LINKEDIT segment */
270 uint32_t datasize
; /* file size of data in __LINKEDIT segment */
273 #define PLATFORM_MACOS 1
275 struct build_version_command
{
276 uint32_t cmd
; /* LC_BUILD_VERSION */
277 uint32_t cmdsize
; /* sizeof(struct build_version_command) plus */
278 /* ntools * sizeof(struct build_tool_version) */
279 uint32_t platform
; /* platform */
280 uint32_t minos
; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
281 uint32_t sdk
; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
282 uint32_t ntools
; /* number of tool entries following this */
285 struct source_version_command
{
286 uint32_t cmd
; /* LC_SOURCE_VERSION */
287 uint32_t cmdsize
; /* 16 */
288 uint64_t version
; /* A.B.C.D.E packed as a24.b10.c10.d10.e10 */
291 struct symtab_command
{
292 uint32_t cmd
; /* LC_SYMTAB */
293 uint32_t cmdsize
; /* sizeof(struct symtab_command) */
294 uint32_t symoff
; /* symbol table offset */
295 uint32_t nsyms
; /* number of symbol table entries */
296 uint32_t stroff
; /* string table offset */
297 uint32_t strsize
; /* string table size in bytes */
300 struct dysymtab_command
{
301 uint32_t cmd
; /* LC_DYSYMTAB */
302 uint32_t cmdsize
; /* sizeof(struct dysymtab_command) */
304 uint32_t ilocalsym
; /* index to local symbols */
305 uint32_t nlocalsym
; /* number of local symbols */
307 uint32_t iextdefsym
;/* index to externally defined symbols */
308 uint32_t nextdefsym
;/* number of externally defined symbols */
310 uint32_t iundefsym
; /* index to undefined symbols */
311 uint32_t nundefsym
; /* number of undefined symbols */
313 uint32_t tocoff
; /* file offset to table of contents */
314 uint32_t ntoc
; /* number of entries in table of contents */
316 uint32_t modtaboff
; /* file offset to module table */
317 uint32_t nmodtab
; /* number of module table entries */
319 uint32_t extrefsymoff
; /* offset to referenced symbol table */
320 uint32_t nextrefsyms
; /* number of referenced symbol table entries */
322 uint32_t indirectsymoff
;/* file offset to the indirect symbol table */
323 uint32_t nindirectsyms
; /* number of indirect symbol table entries */
325 uint32_t extreloff
; /* offset to external relocation entries */
326 uint32_t nextrel
; /* number of external relocation entries */
327 uint32_t locreloff
; /* offset to local relocation entries */
328 uint32_t nlocrel
; /* number of local relocation entries */
331 #define BIND_OPCODE_DONE 0x00
332 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
333 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
334 #define BIND_OPCODE_SET_TYPE_IMM 0x50
335 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
336 #define BIND_OPCODE_DO_BIND 0x90
338 #define BIND_SYMBOL_FLAGS_WEAK_IMPORT 0x1
340 #define BIND_TYPE_POINTER 1
341 #define BIND_SPECIAL_DYLIB_FLAT_LOOKUP -2
343 #define REBASE_OPCODE_DONE 0x00
344 #define REBASE_OPCODE_SET_TYPE_IMM 0x10
345 #define REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x20
346 #define REBASE_OPCODE_DO_REBASE_IMM_TIMES 0x50
348 #define REBASE_TYPE_POINTER 1
350 #define EXPORT_SYMBOL_FLAGS_KIND_REGULAR 0x00
351 #define EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE 0x02
352 #define EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION 0x04
354 struct dyld_info_command
{
355 uint32_t cmd
; /* LC_DYLD_INFO or LC_DYLD_INFO_ONLY */
356 uint32_t cmdsize
; /* sizeof(struct dyld_info_command) */
357 uint32_t rebase_off
; /* file offset to rebase info */
358 uint32_t rebase_size
; /* size of rebase info */
359 uint32_t bind_off
; /* file offset to binding info */
360 uint32_t bind_size
; /* size of binding info */
361 uint32_t weak_bind_off
; /* file offset to weak binding info */
362 uint32_t weak_bind_size
; /* size of weak binding info */
363 uint32_t lazy_bind_off
; /* file offset to lazy binding info */
364 uint32_t lazy_bind_size
; /* size of lazy binding infs */
365 uint32_t export_off
; /* file offset to lazy binding info */
366 uint32_t export_size
; /* size of lazy binding infs */
369 #define INDIRECT_SYMBOL_LOCAL 0x80000000
371 struct entry_point_command
{
372 uint32_t cmd
; /* LC_MAIN only used in MH_EXECUTE filetypes */
373 uint32_t cmdsize
; /* 24 */
374 uint64_t entryoff
; /* file (__TEXT) offset of main() */
375 uint64_t stacksize
;/* if not zero, initial stack size */
386 sk_nl_ptr
, // non-lazy pointers, aka GOT
395 sk_la_ptr
, // lazy pointers
405 uint32_t n_strx
; /* index into the string table */
406 uint8_t n_type
; /* type flag, see below */
407 uint8_t n_sect
; /* section number or NO_SECT */
408 uint16_t n_desc
; /* see <mach-o/stab.h> */
409 uint64_t n_value
; /* value of this symbol (or stab offset) */
417 #define N_WEAK_REF 0x0040
418 #define N_WEAK_DEF 0x0080
421 struct mach_header_64 mh
;
423 struct load_command
**lc
;
424 struct entry_point_command
*ep
;
429 } sk_to_sect
[sk_last
];
432 Section
*symtab
, *strtab
, *indirsyms
, *stubs
, *exports
;
433 uint32_t ilocal
, iextdef
, iundef
;
434 int stubsym
, n_got
, nr_plt
;
435 int segment
[sk_last
];
436 #ifdef CONFIG_NEW_MACHO
437 Section
*chained_fixups
;
446 Section
*rebase
, *binding
, *weak_binding
, *lazy_binding
;
447 Section
*stub_helper
, *la_symbol_ptr
;
448 struct dyld_info_command
*dyldinfo
;
449 int helpsym
, lasym
, dyld_private
, dyld_stub_binder
;
454 int la_symbol_offset
;
470 #define SHT_LINKEDIT (SHT_LOOS + 42)
471 #define SHN_FROMDLL (SHN_LOOS + 2) /* Symbol is undefined, comes from a DLL */
473 static void * add_lc(struct macho
*mo
, uint32_t cmd
, uint32_t cmdsize
)
475 struct load_command
*lc
= tcc_mallocz(cmdsize
);
477 lc
->cmdsize
= cmdsize
;
478 mo
->lc
= tcc_realloc(mo
->lc
, sizeof(mo
->lc
[0]) * (mo
->nlc
+ 1));
479 mo
->lc
[mo
->nlc
++] = lc
;
483 static struct segment_command_64
* add_segment(struct macho
*mo
, const char *name
)
485 struct segment_command_64
*sc
= add_lc(mo
, LC_SEGMENT_64
, sizeof(*sc
));
486 strncpy(sc
->segname
, name
, 16);
487 mo
->seg2lc
= tcc_realloc(mo
->seg2lc
, sizeof(*mo
->seg2lc
) * (mo
->nseg
+ 1));
488 mo
->seg2lc
[mo
->nseg
++] = mo
->nlc
- 1;
492 static struct segment_command_64
* get_segment(struct macho
*mo
, int i
)
494 return (struct segment_command_64
*) (mo
->lc
[mo
->seg2lc
[i
]]);
497 static int add_section(struct macho
*mo
, struct segment_command_64
**_seg
, const char *name
)
499 struct segment_command_64
*seg
= *_seg
;
500 int ret
= seg
->nsects
;
501 struct section_64
*sec
;
503 seg
->cmdsize
+= sizeof(*sec
);
504 seg
= tcc_realloc(seg
, sizeof(*seg
) + seg
->nsects
* sizeof(*sec
));
505 sec
= (struct section_64
*)((char*)seg
+ sizeof(*seg
)) + ret
;
506 memset(sec
, 0, sizeof(*sec
));
507 strncpy(sec
->sectname
, name
, 16);
508 strncpy(sec
->segname
, seg
->segname
, 16);
513 static struct section_64
*get_section(struct segment_command_64
*seg
, int i
)
515 return (struct section_64
*)((char*)seg
+ sizeof(*seg
)) + i
;
518 static void * add_dylib(struct macho
*mo
, char *name
)
520 struct dylib_command
*lc
;
521 int sz
= (sizeof(*lc
) + strlen(name
) + 1 + 7) & -8;
522 lc
= add_lc(mo
, LC_LOAD_DYLIB
, sz
);
523 lc
->name
= sizeof(*lc
);
524 strcpy((char*)lc
+ lc
->name
, name
);
526 lc
->current_version
= 1 << 16;
527 lc
->compatibility_version
= 1 << 16;
531 static int uleb128_size (unsigned long long value
)
538 } while (value
!= 0);
542 static void write_uleb128(Section
*section
, uint64_t value
)
545 unsigned char byte
= value
& 0x7f;
546 uint8_t *ptr
= section_ptr_add(section
, 1);
549 *ptr
= byte
| (value
? 0x80 : 0);
550 } while (value
!= 0);
553 static void tcc_macho_add_destructor(TCCState
*s1
)
555 int init_sym
, mh_execute_header
, at_exit_sym
;
560 mh_execute_header
= put_elf_sym(s1
->symtab
, -4096, 0,
561 ELFW(ST_INFO
)(STB_GLOBAL
, STT_OBJECT
), 0,
562 text_section
->sh_num
, "__mh_execute_header");
563 s
= find_section(s1
, ".fini_array");
564 if (s
->data_offset
== 0)
566 init_sym
= put_elf_sym(s1
->symtab
, text_section
->data_offset
, 0,
567 ELFW(ST_INFO
)(STB_LOCAL
, STT_FUNC
), 0,
568 text_section
->sh_num
, "___GLOBAL_init_65535");
569 at_exit_sym
= put_elf_sym(s1
->symtab
, 0, 0,
570 ELFW(ST_INFO
)(STB_GLOBAL
, STT_FUNC
), 0,
571 SHN_UNDEF
, "___cxa_atexit");
572 #ifdef TCC_TARGET_X86_64
573 ptr
= section_ptr_add(text_section
, 4);
574 ptr
[0] = 0x55; // pushq %rbp
575 ptr
[1] = 0x48; // movq %rsp, %rbp
578 for_each_elem(s
->reloc
, 0, rel
, ElfW_Rel
) {
579 int sym_index
= ELFW(R_SYM
)(rel
->r_info
);
581 ptr
= section_ptr_add(text_section
, 26);
582 ptr
[0] = 0x48; // lea destructor(%rip),%rax
585 put_elf_reloca(s1
->symtab
, text_section
,
586 text_section
->data_offset
- 23,
587 R_X86_64_PC32
, sym_index
, -4);
588 ptr
[7] = 0x48; // mov %rax,%rdi
591 ptr
[10] = 0x31; // xorl %ecx, %ecx
593 ptr
[12] = 0x89; // movl %ecx, %esi
595 ptr
[14] = 0x48; // lea mh_execute_header(%rip),%rdx
598 put_elf_reloca(s1
->symtab
, text_section
,
599 text_section
->data_offset
- 9,
600 R_X86_64_PC32
, mh_execute_header
, -4);
601 ptr
[21] = 0xe8; // call __cxa_atexit
602 put_elf_reloca(s1
->symtab
, text_section
,
603 text_section
->data_offset
- 4,
604 R_X86_64_PLT32
, at_exit_sym
, -4);
606 ptr
= section_ptr_add(text_section
, 2);
607 ptr
[0] = 0x5d; // pop %rbp
608 ptr
[1] = 0xc3; // ret
609 #elif defined TCC_TARGET_ARM64
610 ptr
= section_ptr_add(text_section
, 8);
611 write32le(ptr
, 0xa9bf7bfd); // stp x29, x30, [sp, #-16]!
612 write32le(ptr
+ 4, 0x910003fd); // mov x29, sp
613 for_each_elem(s
->reloc
, 0, rel
, ElfW_Rel
) {
614 int sym_index
= ELFW(R_SYM
)(rel
->r_info
);
616 ptr
= section_ptr_add(text_section
, 24);
617 put_elf_reloc(s1
->symtab
, text_section
,
618 text_section
->data_offset
- 24,
619 R_AARCH64_ADR_PREL_PG_HI21
, sym_index
);
620 write32le(ptr
, 0x90000000); // adrp x0, destructor@page
621 put_elf_reloc(s1
->symtab
, text_section
,
622 text_section
->data_offset
- 20,
623 R_AARCH64_LDST8_ABS_LO12_NC
, sym_index
);
624 write32le(ptr
+ 4, 0x91000000); // add x0,x0,destructor@pageoff
625 write32le(ptr
+ 8, 0xd2800001); // mov x1, #0
626 put_elf_reloc(s1
->symtab
, text_section
,
627 text_section
->data_offset
- 12,
628 R_AARCH64_ADR_PREL_PG_HI21
, mh_execute_header
);
629 write32le(ptr
+ 12, 0x90000002); // adrp x2, mh_execute_header@page
630 put_elf_reloc(s1
->symtab
, text_section
,
631 text_section
->data_offset
- 8,
632 R_AARCH64_LDST8_ABS_LO12_NC
, mh_execute_header
);
633 write32le(ptr
+ 16, 0x91000042); // add x2,x2,mh_execute_header@pageoff
634 put_elf_reloc(s1
->symtab
, text_section
,
635 text_section
->data_offset
- 4,
636 R_AARCH64_CALL26
, at_exit_sym
);
637 write32le(ptr
+ 20, 0x94000000); // bl __cxa_atexit
639 ptr
= section_ptr_add(text_section
, 8);
640 write32le(ptr
, 0xa8c17bfd); // ldp x29, x30, [sp], #16
641 write32le(ptr
+ 4, 0xd65f03c0); // ret
643 s
->reloc
->data_offset
= s
->data_offset
= 0;
644 s
->sh_flags
&= ~SHF_ALLOC
;
645 add_array (s1
, ".init_array", init_sym
);
648 #ifdef CONFIG_NEW_MACHO
649 static void bind_rebase_add(struct macho
*mo
, int bind
, int sh_info
,
650 ElfW_Rel
*rel
, struct sym_attr
*attr
)
652 mo
->bind_rebase
= tcc_realloc(mo
->bind_rebase
, (mo
->n_bind_rebase
+ 1) *
653 sizeof(struct bind_rebase
));
654 mo
->bind_rebase
[mo
->n_bind_rebase
].section
= sh_info
;
655 mo
->bind_rebase
[mo
->n_bind_rebase
].bind
= bind
;
656 mo
->bind_rebase
[mo
->n_bind_rebase
].rel
= *rel
;
658 mo
->bind_rebase
[mo
->n_bind_rebase
].rel
.r_offset
= attr
->got_offset
;
663 static void check_relocs(TCCState
*s1
, struct macho
*mo
)
666 ElfW_Rel
*rel
, save_rel
;
668 int i
, j
, type
, gotplt_entry
, sym_index
, for_code
;
670 struct sym_attr
*attr
;
673 mo
->nr_plt
= mo
->n_got
= 0;
674 for (i
= 1; i
< s1
->nb_sections
; i
++) {
676 if (s
->sh_type
!= SHT_RELX
||
677 !strncmp(s1
->sections
[s
->sh_info
]->name
, ".debug_", 7))
679 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
681 type
= ELFW(R_TYPE
)(rel
->r_info
);
682 gotplt_entry
= gotplt_entry_type(type
);
683 for_code
= code_reloc(type
);
684 /* We generate a non-lazy pointer for used undefined symbols
685 and for defined symbols that must have a place for their
686 address due to codegen (i.e. a reloc requiring a got slot). */
687 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
688 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
689 if (sym
->st_shndx
== SHN_UNDEF
690 || gotplt_entry
== ALWAYS_GOTPLT_ENTRY
) {
691 attr
= get_sym_attr(s1
, sym_index
, 1);
692 if (!attr
->dyn_index
) {
693 attr
->got_offset
= s1
->got
->data_offset
;
694 attr
->plt_offset
= -1;
695 attr
->dyn_index
= 1; /* used as flag */
696 section_ptr_add(s1
->got
, PTR_SIZE
);
697 put_elf_reloc(s1
->symtab
, s1
->got
, attr
->got_offset
,
698 R_JMP_SLOT
, sym_index
);
699 goti
= tcc_realloc(goti
, (mo
->n_got
+ 1) * sizeof(*goti
));
700 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
) {
701 if (sym
->st_shndx
== SHN_UNDEF
)
702 tcc_error("undefined local symbo: '%s'",
703 (char *) symtab_section
->link
->data
+ sym
->st_name
);
704 goti
[mo
->n_got
++] = INDIRECT_SYMBOL_LOCAL
;
706 goti
[mo
->n_got
++] = mo
->e2msym
[sym_index
];
707 if (sym
->st_shndx
== SHN_UNDEF
708 #ifdef TCC_TARGET_X86_64
709 && type
== R_X86_64_GOTPCREL
710 #elif defined TCC_TARGET_ARM64
711 && type
== R_AARCH64_ADR_GOT_PAGE
714 attr
->plt_offset
= -mo
->n_bind_rebase
- 2;
715 bind_rebase_add(mo
, 1, s1
->got
->reloc
->sh_info
, &save_rel
, attr
);
716 s1
->got
->reloc
->data_offset
-= sizeof (ElfW_Rel
);
718 if (for_code
&& sym
->st_shndx
== SHN_UNDEF
)
719 s1
->got
->reloc
->data_offset
-= sizeof (ElfW_Rel
);
722 if (for_code
&& sym
->st_shndx
== SHN_UNDEF
) {
723 if ((int)attr
->plt_offset
< -1) {
724 /* remove above bind and replace with plt */
725 mo
->bind_rebase
[-attr
->plt_offset
- 2].bind
= 2;
726 attr
->plt_offset
= -1;
728 if (attr
->plt_offset
== -1) {
731 attr
->plt_offset
= mo
->stubs
->data_offset
;
732 #ifdef TCC_TARGET_X86_64
733 if (type
!= R_X86_64_PLT32
)
735 jmp
= section_ptr_add(mo
->stubs
, 6);
736 jmp
[0] = 0xff; /* jmpq *ofs(%rip) */
738 put_elf_reloc(s1
->symtab
, mo
->stubs
,
739 attr
->plt_offset
+ 2,
740 R_X86_64_GOTPCREL
, sym_index
);
741 #elif defined TCC_TARGET_ARM64
742 if (type
!= R_AARCH64_CALL26
)
744 jmp
= section_ptr_add(mo
->stubs
, 12);
745 put_elf_reloc(s1
->symtab
, mo
->stubs
,
747 R_AARCH64_ADR_GOT_PAGE
, sym_index
);
748 write32le(jmp
, // adrp x16, #sym
750 put_elf_reloc(s1
->symtab
, mo
->stubs
,
751 attr
->plt_offset
+ 4,
752 R_AARCH64_LD64_GOT_LO12_NC
, sym_index
);
753 write32le(jmp
+ 4, // ld x16,[x16, #sym]
755 write32le(jmp
+ 8, // br x16
758 bind_rebase_add(mo
, 1, s1
->got
->reloc
->sh_info
, &save_rel
, attr
);
759 pi
= section_ptr_add(mo
->indirsyms
, sizeof(*pi
));
760 *pi
= mo
->e2msym
[sym_index
];
763 rel
->r_info
= ELFW(R_INFO
)(mo
->stubsym
, type
);
764 rel
->r_addend
+= attr
->plt_offset
;
767 if (type
== R_DATA_PTR
|| type
== R_JMP_SLOT
)
768 bind_rebase_add(mo
, sym
->st_shndx
== SHN_UNDEF
? 1 : 0,
769 s
->sh_info
, &save_rel
, NULL
);
772 /* remove deleted binds */
773 for (i
= 0, j
= 0; i
< mo
->n_bind_rebase
; i
++)
774 if (mo
->bind_rebase
[i
].bind
== 2)
777 mo
->bind_rebase
[j
++] = mo
->bind_rebase
[i
];
778 mo
->n_bind_rebase
= j
;
779 pi
= section_ptr_add(mo
->indirsyms
, mo
->n_got
* sizeof(*pi
));
780 memcpy(pi
, goti
, mo
->n_got
* sizeof(*pi
));
786 static void check_relocs(TCCState
*s1
, struct macho
*mo
)
790 ElfW_Rel
*rel
, save_rel
;
792 int i
, type
, gotplt_entry
, sym_index
, for_code
;
793 int bind_offset
, la_symbol_offset
;
795 struct sym_attr
*attr
;
797 #ifdef TCC_TARGET_X86_64
798 jmp
= section_ptr_add(mo
->stub_helper
, 16);
799 jmp
[0] = 0x4c; /* leaq _dyld_private(%rip), %r11 */
802 put_elf_reloca(s1
->symtab
, mo
->stub_helper
, 3,
803 R_X86_64_PC32
, mo
->dyld_private
, -4);
804 jmp
[7] = 0x41; /* pushq %r11 */
806 jmp
[9] = 0xff; /* jmpq *dyld_stub_binder@GOT(%rip) */
808 put_elf_reloca(s1
->symtab
, mo
->stub_helper
, 11,
809 R_X86_64_GOTPCREL
, mo
->dyld_stub_binder
, -4);
810 jmp
[15] = 0x90; /* nop */
811 #elif defined TCC_TARGET_ARM64
812 jmp
= section_ptr_add(mo
->stub_helper
, 24);
813 put_elf_reloc(s1
->symtab
, mo
->stub_helper
, 0,
814 R_AARCH64_ADR_PREL_PG_HI21
, mo
->dyld_private
);
815 write32le(jmp
, 0x90000011); // adrp x17, _dyld_private@page
816 put_elf_reloc(s1
->symtab
, mo
->stub_helper
, 4,
817 R_AARCH64_LDST64_ABS_LO12_NC
, mo
->dyld_private
);
818 write32le(jmp
+ 4, 0x91000231); // add x17,x17,_dyld_private@pageoff
819 write32le(jmp
+ 8, 0xa9bf47f0); // stp x16/x17, [sp, #-16]!
820 put_elf_reloc(s1
->symtab
, mo
->stub_helper
, 12,
821 R_AARCH64_ADR_GOT_PAGE
, mo
->dyld_stub_binder
);
822 write32le(jmp
+ 12, 0x90000010); // adrp x16, dyld_stub_binder@page
823 put_elf_reloc(s1
->symtab
, mo
->stub_helper
, 16,
824 R_AARCH64_LD64_GOT_LO12_NC
, mo
->dyld_stub_binder
);
825 write32le(jmp
+ 16, 0xf9400210); // ldr x16,[x16,dyld_stub_binder@pageoff]
826 write32le(jmp
+ 20, 0xd61f0200); // br x16
830 mo
->nr_plt
= mo
->n_got
= 0;
831 for (i
= 1; i
< s1
->nb_sections
; i
++) {
833 if (s
->sh_type
!= SHT_RELX
||
834 !strncmp(s1
->sections
[s
->sh_info
]->name
, ".debug_", 7))
836 for_each_elem(s
, 0, rel
, ElfW_Rel
) {
838 type
= ELFW(R_TYPE
)(rel
->r_info
);
839 gotplt_entry
= gotplt_entry_type(type
);
840 for_code
= code_reloc(type
);
841 /* We generate a non-lazy pointer for used undefined symbols
842 and for defined symbols that must have a place for their
843 address due to codegen (i.e. a reloc requiring a got slot). */
844 sym_index
= ELFW(R_SYM
)(rel
->r_info
);
845 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
846 if (sym
->st_shndx
== SHN_UNDEF
847 || gotplt_entry
== ALWAYS_GOTPLT_ENTRY
) {
848 attr
= get_sym_attr(s1
, sym_index
, 1);
849 if (!attr
->dyn_index
) {
850 attr
->got_offset
= s1
->got
->data_offset
;
851 attr
->plt_offset
= -1;
852 attr
->dyn_index
= 1; /* used as flag */
853 section_ptr_add(s1
->got
, PTR_SIZE
);
854 put_elf_reloc(s1
->symtab
, s1
->got
, attr
->got_offset
,
855 R_JMP_SLOT
, sym_index
);
856 goti
= tcc_realloc(goti
, (mo
->n_got
+ 1) * sizeof(*goti
));
857 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_LOCAL
) {
858 if (sym
->st_shndx
== SHN_UNDEF
)
859 tcc_error("undefined local symbo: '%s'",
860 (char *) symtab_section
->link
->data
+ sym
->st_name
);
861 goti
[mo
->n_got
++] = INDIRECT_SYMBOL_LOCAL
;
863 goti
[mo
->n_got
++] = mo
->e2msym
[sym_index
];
864 if (sym
->st_shndx
== SHN_UNDEF
865 #ifdef TCC_TARGET_X86_64
866 && type
== R_X86_64_GOTPCREL
867 #elif defined TCC_TARGET_ARM64
868 && type
== R_AARCH64_ADR_GOT_PAGE
872 tcc_realloc(mo
->bind
,
874 sizeof(struct bind
));
875 mo
->bind
[mo
->n_bind
].section
= s1
->got
->reloc
->sh_info
;
876 mo
->bind
[mo
->n_bind
].rel
= save_rel
;
877 mo
->bind
[mo
->n_bind
].rel
.r_offset
= attr
->got_offset
;
879 s1
->got
->reloc
->data_offset
-= sizeof (ElfW_Rel
);
883 if (for_code
&& sym
->st_shndx
== SHN_UNDEF
) {
884 if (attr
->plt_offset
== -1) {
885 attr
->plt_offset
= mo
->stubs
->data_offset
;
886 #ifdef TCC_TARGET_X86_64
887 if (type
!= R_X86_64_PLT32
)
890 jmp
= section_ptr_add(mo
->stubs
, 6);
891 jmp
[0] = 0xff; /* jmpq *__la_symbol_ptr(%rip) */
893 put_elf_reloca(s1
->symtab
, mo
->stubs
,
894 mo
->stubs
->data_offset
- 4,
895 R_X86_64_PC32
, mo
->lasym
,
896 mo
->la_symbol_ptr
->data_offset
- 4);
899 bind_offset
= mo
->stub_helper
->data_offset
+ 1;
900 jmp
= section_ptr_add(mo
->stub_helper
, 10);
901 jmp
[0] = 0x68; /* pushq $bind_offset */
902 jmp
[5] = 0xe9; /* jmpq __stub_helper */
903 write32le(jmp
+ 6, -mo
->stub_helper
->data_offset
);
905 /* __la_symbol_ptr */
906 la_symbol_offset
= mo
->la_symbol_ptr
->data_offset
;
907 put_elf_reloca(s1
->symtab
, mo
->la_symbol_ptr
,
908 mo
->la_symbol_ptr
->data_offset
,
909 R_DATA_PTR
, mo
->helpsym
,
910 mo
->stub_helper
->data_offset
- 10);
911 section_ptr_add(mo
->la_symbol_ptr
, PTR_SIZE
);
912 #elif defined TCC_TARGET_ARM64
913 if (type
!= R_AARCH64_CALL26
)
916 jmp
= section_ptr_add(mo
->stubs
, 12);
917 put_elf_reloca(s1
->symtab
, mo
->stubs
,
918 mo
->stubs
->data_offset
- 12,
919 R_AARCH64_ADR_PREL_PG_HI21
, mo
->lasym
,
920 mo
->la_symbol_ptr
->data_offset
);
921 write32le(jmp
, // adrp x16, __la_symbol_ptr@page
923 put_elf_reloca(s1
->symtab
, mo
->stubs
,
924 mo
->stubs
->data_offset
- 8,
925 R_AARCH64_LDST64_ABS_LO12_NC
, mo
->lasym
,
926 mo
->la_symbol_ptr
->data_offset
);
927 write32le(jmp
+ 4, // ldr x16,[x16, __la_symbol_ptr@pageoff]
929 write32le(jmp
+ 8, // br x16
933 bind_offset
= mo
->stub_helper
->data_offset
+ 8;
934 jmp
= section_ptr_add(mo
->stub_helper
, 12);
935 write32le(jmp
+ 0, // ldr w16, l0
937 write32le(jmp
+ 4, // b stubHelperHeader
939 ((-(mo
->stub_helper
->data_offset
- 8) / 4) &
941 write32le(jmp
+ 8, 0); // l0: .long bind_offset
943 /* __la_symbol_ptr */
944 la_symbol_offset
= mo
->la_symbol_ptr
->data_offset
;
945 put_elf_reloca(s1
->symtab
, mo
->la_symbol_ptr
,
946 mo
->la_symbol_ptr
->data_offset
,
947 R_DATA_PTR
, mo
->helpsym
,
948 mo
->stub_helper
->data_offset
- 12);
949 section_ptr_add(mo
->la_symbol_ptr
, PTR_SIZE
);
952 tcc_realloc(mo
->s_lazy_bind
, (mo
->n_lazy_bind
+ 1) *
953 sizeof(struct s_lazy_bind
));
954 mo
->s_lazy_bind
[mo
->n_lazy_bind
].section
=
955 mo
->stub_helper
->reloc
->sh_info
;
956 mo
->s_lazy_bind
[mo
->n_lazy_bind
].bind_offset
=
958 mo
->s_lazy_bind
[mo
->n_lazy_bind
].la_symbol_offset
=
960 mo
->s_lazy_bind
[mo
->n_lazy_bind
].rel
= save_rel
;
961 mo
->s_lazy_bind
[mo
->n_lazy_bind
].rel
.r_offset
=
964 pi
= section_ptr_add(mo
->indirsyms
, sizeof(*pi
));
965 *pi
= mo
->e2msym
[sym_index
];
968 rel
->r_info
= ELFW(R_INFO
)(mo
->stubsym
, type
);
969 rel
->r_addend
+= attr
->plt_offset
;
972 if (type
== R_DATA_PTR
|| type
== R_JMP_SLOT
) {
973 if (sym
->st_shndx
== SHN_UNDEF
) {
974 mo
->bind
= tcc_realloc(mo
->bind
,
976 sizeof(struct bind
));
977 mo
->bind
[mo
->n_bind
].section
= s
->sh_info
;
978 mo
->bind
[mo
->n_bind
].rel
= save_rel
;
983 tcc_realloc(mo
->s_rebase
, (mo
->n_rebase
+ 1) *
984 sizeof(struct s_rebase
));
985 mo
->s_rebase
[mo
->n_rebase
].section
= s
->sh_info
;
986 mo
->s_rebase
[mo
->n_rebase
].rel
= save_rel
;
992 pi
= section_ptr_add(mo
->indirsyms
, mo
->n_got
* sizeof(*pi
));
993 memcpy(pi
, goti
, mo
->n_got
* sizeof(*pi
));
994 pi
= section_ptr_add(mo
->indirsyms
, mo
->nr_plt
* sizeof(*pi
));
995 memcpy(pi
, mo
->indirsyms
->data
, mo
->nr_plt
* sizeof(*pi
));
1000 static int check_symbols(TCCState
*s1
, struct macho
*mo
)
1002 int sym_index
, sym_end
;
1005 mo
->ilocal
= mo
->iextdef
= mo
->iundef
= -1;
1006 sym_end
= symtab_section
->data_offset
/ sizeof(ElfW(Sym
));
1007 for (sym_index
= 1; sym_index
< sym_end
; ++sym_index
) {
1008 int elf_index
= ((struct nlist_64
*)mo
->symtab
->data
+ sym_index
- 1)->n_value
;
1009 ElfW(Sym
) *sym
= (ElfW(Sym
) *)symtab_section
->data
+ elf_index
;
1010 const char *name
= (char*)symtab_section
->link
->data
+ sym
->st_name
;
1011 unsigned type
= ELFW(ST_TYPE
)(sym
->st_info
);
1012 unsigned bind
= ELFW(ST_BIND
)(sym
->st_info
);
1013 unsigned vis
= ELFW(ST_VISIBILITY
)(sym
->st_other
);
1015 dprintf("%4d (%4d): %09lx %4d %4d %4d %3d %s\n",
1016 sym_index
, elf_index
, (long)sym
->st_value
,
1017 type
, bind
, vis
, sym
->st_shndx
, name
);
1018 if (bind
== STB_LOCAL
) {
1019 if (mo
->ilocal
== -1)
1020 mo
->ilocal
= sym_index
- 1;
1021 if (mo
->iextdef
!= -1 || mo
->iundef
!= -1)
1022 tcc_error("local syms after global ones");
1023 } else if (sym
->st_shndx
!= SHN_UNDEF
) {
1024 if (mo
->iextdef
== -1)
1025 mo
->iextdef
= sym_index
- 1;
1026 if (mo
->iundef
!= -1)
1027 tcc_error("external defined symbol after undefined");
1028 } else if (sym
->st_shndx
== SHN_UNDEF
) {
1029 if (mo
->iundef
== -1)
1030 mo
->iundef
= sym_index
- 1;
1031 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
1032 || s1
->output_type
!= TCC_OUTPUT_EXE
1033 || find_elf_sym(s1
->dynsymtab_section
, name
)) {
1034 /* Mark the symbol as coming from a dylib so that
1035 relocate_syms doesn't complain. Normally bind_exe_dynsyms
1036 would do this check, and place the symbol into dynsym
1037 which is checked by relocate_syms. But Mach-O doesn't use
1038 bind_exe_dynsyms. */
1039 sym
->st_shndx
= SHN_FROMDLL
;
1042 tcc_error_noabort("undefined symbol '%s'", name
);
1049 static void convert_symbol(TCCState
*s1
, struct macho
*mo
, struct nlist_64
*pn
)
1051 struct nlist_64 n
= *pn
;
1052 ElfSym
*sym
= (ElfW(Sym
) *)symtab_section
->data
+ pn
->n_value
;
1053 const char *name
= (char*)symtab_section
->link
->data
+ sym
->st_name
;
1054 switch(ELFW(ST_TYPE
)(sym
->st_info
)) {
1065 tcc_error("unhandled ELF symbol type %d %s",
1066 ELFW(ST_TYPE
)(sym
->st_info
), name
);
1068 if (sym
->st_shndx
== SHN_UNDEF
)
1069 tcc_error("should have been rewritten to SHN_FROMDLL: %s", name
);
1070 else if (sym
->st_shndx
== SHN_FROMDLL
)
1071 n
.n_type
= N_UNDF
, n
.n_sect
= 0;
1072 else if (sym
->st_shndx
== SHN_ABS
)
1073 n
.n_type
= N_ABS
, n
.n_sect
= 0;
1074 else if (sym
->st_shndx
>= SHN_LORESERVE
)
1075 tcc_error("unhandled ELF symbol section %d %s", sym
->st_shndx
, name
);
1076 else if (!mo
->elfsectomacho
[sym
->st_shndx
]) {
1077 if (strncmp(s1
->sections
[sym
->st_shndx
]->name
, ".debug_", 7))
1078 tcc_error("ELF section %d(%s) not mapped into Mach-O for symbol %s",
1079 sym
->st_shndx
, s1
->sections
[sym
->st_shndx
]->name
, name
);
1082 n
.n_sect
= mo
->elfsectomacho
[sym
->st_shndx
];
1083 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_GLOBAL
)
1085 else if (ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
)
1086 n
.n_desc
|= N_WEAK_REF
| (n
.n_type
!= N_UNDF
? N_WEAK_DEF
: 0);
1087 n
.n_strx
= pn
->n_strx
;
1088 n
.n_value
= sym
->st_value
;
1092 static void convert_symbols(TCCState
*s1
, struct macho
*mo
)
1094 struct nlist_64
*pn
;
1095 for_each_elem(mo
->symtab
, 0, pn
, struct nlist_64
)
1096 convert_symbol(s1
, mo
, pn
);
1099 static int machosymcmp(const void *_a
, const void *_b
, void *arg
)
1102 int ea
= ((struct nlist_64
*)_a
)->n_value
;
1103 int eb
= ((struct nlist_64
*)_b
)->n_value
;
1104 ElfSym
*sa
= (ElfSym
*)symtab_section
->data
+ ea
;
1105 ElfSym
*sb
= (ElfSym
*)symtab_section
->data
+ eb
;
1107 /* locals, then defined externals, then undefined externals, the
1108 last two sections also by name, otherwise stable sort */
1109 r
= (ELFW(ST_BIND
)(sb
->st_info
) == STB_LOCAL
)
1110 - (ELFW(ST_BIND
)(sa
->st_info
) == STB_LOCAL
);
1113 r
= (sa
->st_shndx
== SHN_UNDEF
) - (sb
->st_shndx
== SHN_UNDEF
);
1116 if (ELFW(ST_BIND
)(sa
->st_info
) != STB_LOCAL
) {
1117 const char * na
= (char*)symtab_section
->link
->data
+ sa
->st_name
;
1118 const char * nb
= (char*)symtab_section
->link
->data
+ sb
->st_name
;
1126 /* cannot use qsort because code has to be reentrant */
1127 static void tcc_qsort (void *base
, size_t nel
, size_t width
,
1128 int (*comp
)(const void *, const void *, void *), void *arg
)
1130 size_t wnel
, gap
, wgap
, i
, j
, k
;
1134 for (gap
= 0; ++gap
< nel
;)
1136 while ( gap
/= 3 ) {
1138 for (i
= wgap
; i
< wnel
; i
+= width
) {
1139 for (j
= i
- wgap
; ;j
-= wgap
) {
1140 a
= j
+ (char *)base
;
1142 if ( (*comp
)(a
, b
, arg
) <= 0 )
1157 static void create_symtab(TCCState
*s1
, struct macho
*mo
)
1159 int sym_index
, sym_end
;
1160 struct nlist_64
*pn
;
1162 /* Stub creation belongs to check_relocs, but we need to create
1163 the symbol now, so its included in the sorting. */
1164 mo
->stubs
= new_section(s1
, "__stubs", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
1165 s1
->got
= new_section(s1
, ".got", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
1166 mo
->stubsym
= put_elf_sym(s1
->symtab
, 0, 0,
1167 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
1168 mo
->stubs
->sh_num
, ".__stubs");
1169 #ifdef CONFIG_NEW_MACHO
1170 mo
->chained_fixups
= new_section(s1
, "CHAINED_FIXUPS",
1171 SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1173 mo
->stub_helper
= new_section(s1
, "__stub_helper", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
1174 mo
->la_symbol_ptr
= new_section(s1
, "__la_symbol_ptr", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
1175 mo
->helpsym
= put_elf_sym(s1
->symtab
, 0, 0,
1176 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
1177 mo
->stub_helper
->sh_num
, ".__stub_helper");
1178 mo
->lasym
= put_elf_sym(s1
->symtab
, 0, 0,
1179 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
1180 mo
->la_symbol_ptr
->sh_num
, ".__la_symbol_ptr");
1181 section_ptr_add(data_section
, -data_section
->data_offset
& (PTR_SIZE
- 1));
1182 mo
->dyld_private
= put_elf_sym(s1
->symtab
, data_section
->data_offset
, PTR_SIZE
,
1183 ELFW(ST_INFO
)(STB_LOCAL
, STT_OBJECT
), 0,
1184 data_section
->sh_num
, ".__dyld_private");
1185 section_ptr_add(data_section
, PTR_SIZE
);
1186 mo
->dyld_stub_binder
= put_elf_sym(s1
->symtab
, 0, 0,
1187 ELFW(ST_INFO
)(STB_GLOBAL
, STT_OBJECT
), 0,
1188 SHN_UNDEF
, "dyld_stub_binder");
1189 mo
->rebase
= new_section(s1
, "REBASE", SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1190 mo
->binding
= new_section(s1
, "BINDING", SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1191 mo
->weak_binding
= new_section(s1
, "WEAK_BINDING", SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1192 mo
->lazy_binding
= new_section(s1
, "LAZY_BINDING", SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1194 mo
->exports
= new_section(s1
, "EXPORT", SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1195 mo
->indirsyms
= new_section(s1
, "LEINDIR", SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1197 mo
->symtab
= new_section(s1
, "LESYMTAB", SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1198 mo
->strtab
= new_section(s1
, "LESTRTAB", SHT_LINKEDIT
, SHF_ALLOC
| SHF_WRITE
);
1199 put_elf_str(mo
->strtab
, " "); /* Mach-O starts strtab with a space */
1200 sym_end
= symtab_section
->data_offset
/ sizeof(ElfW(Sym
));
1201 pn
= section_ptr_add(mo
->symtab
, sizeof(*pn
) * (sym_end
- 1));
1202 for (sym_index
= 1; sym_index
< sym_end
; ++sym_index
) {
1203 ElfW(Sym
) *sym
= (ElfW(Sym
) *)symtab_section
->data
+ sym_index
;
1204 const char *name
= (char*)symtab_section
->link
->data
+ sym
->st_name
;
1205 pn
[sym_index
- 1].n_strx
= put_elf_str(mo
->strtab
, name
);
1206 pn
[sym_index
- 1].n_value
= sym_index
;
1208 section_ptr_add(mo
->strtab
, -mo
->strtab
->data_offset
& (PTR_SIZE
- 1));
1209 tcc_qsort(pn
, sym_end
- 1, sizeof(*pn
), machosymcmp
, s1
);
1210 mo
->e2msym
= tcc_malloc(sym_end
* sizeof(*mo
->e2msym
));
1212 for (sym_index
= 1; sym_index
< sym_end
; ++sym_index
) {
1213 mo
->e2msym
[pn
[sym_index
- 1].n_value
] = sym_index
- 1;
1221 } skinfo
[sk_last
] = {
1222 /*[sk_unknown] =*/ { 0 },
1223 /*[sk_discard] =*/ { 0 },
1224 /*[sk_text] =*/ { 1, S_REGULAR
| S_ATTR_PURE_INSTRUCTIONS
1225 | S_ATTR_SOME_INSTRUCTIONS
, "__text" },
1226 /*[sk_stubs] =*/ { 1, S_REGULAR
| S_ATTR_PURE_INSTRUCTIONS
| S_SYMBOL_STUBS
1227 | S_ATTR_SOME_INSTRUCTIONS
, "__stubs" },
1228 /*[sk_stub_helper] =*/ { 1, S_REGULAR
| S_ATTR_PURE_INSTRUCTIONS
1229 | S_ATTR_SOME_INSTRUCTIONS
, "__stub_helper" },
1230 /*[sk_ro_data] =*/ { 2, S_REGULAR
, "__rodata" },
1231 /*[sk_uw_info] =*/ { 0 },
1232 /*[sk_nl_ptr] =*/ { 2, S_NON_LAZY_SYMBOL_POINTERS
, "__got" },
1233 /*[sk_debug_info] =*/ { 3, S_REGULAR
| S_ATTR_DEBUG
, "__debug_info" },
1234 /*[sk_debug_abbrev] =*/ { 3, S_REGULAR
| S_ATTR_DEBUG
, "__debug_abbrev" },
1235 /*[sk_debug_line] =*/ { 3, S_REGULAR
| S_ATTR_DEBUG
, "__debug_line" },
1236 /*[sk_debug_aranges] =*/ { 3, S_REGULAR
| S_ATTR_DEBUG
, "__debug_aranges" },
1237 /*[sk_debug_str] =*/ { 3, S_REGULAR
| S_ATTR_DEBUG
, "__debug_str" },
1238 /*[sk_debug_line_str] =*/ { 3, S_REGULAR
| S_ATTR_DEBUG
, "__debug_line_str" },
1239 /*[sk_stab] =*/ { 4, S_REGULAR
, "__stab" },
1240 /*[sk_stab_str] =*/ { 4, S_REGULAR
, "__stab_str" },
1241 /*[sk_la_ptr] =*/ { 4, S_LAZY_SYMBOL_POINTERS
, "__la_symbol_ptr" },
1242 /*[sk_init] =*/ { 4, S_MOD_INIT_FUNC_POINTERS
, "__mod_init_func" },
1243 /*[sk_fini] =*/ { 4, S_MOD_TERM_FUNC_POINTERS
, "__mod_term_func" },
1244 /*[sk_rw_data] =*/ { 4, S_REGULAR
, "__data" },
1245 /*[sk_bss] =*/ { 4, S_ZEROFILL
, "__bss" },
1246 /*[sk_linkedit] =*/ { 5, S_REGULAR
, NULL
},
1249 #define START ((uint64_t)1 << 32)
1260 { 1, "__PAGEZERO", 0, START
, 0, 0, 0 },
1261 { 0, "__TEXT", START
, 0, 5, 5, 0 },
1262 { 0, "__DATA_CONST", -1, 0, 3, 3, SG_READ_ONLY
},
1263 { 0, "__DWARF", -1, 0, 7, 3, 0 },
1264 { 0, "__DATA", -1, 0, 3, 3, 0 },
1265 { 1, "__LINKEDIT", -1, 0, 1, 1, 0 },
1268 #define N_SEGMENT (sizeof(all_segment)/sizeof(all_segment[0]))
1270 #ifdef CONFIG_NEW_MACHO
1271 static void calc_fixup_size(TCCState
*s1
, struct macho
*mo
)
1275 size
= (sizeof(struct dyld_chained_fixups_header
) + 7) & -8;
1276 size
+= (sizeof(struct dyld_chained_starts_in_image
) + (mo
->nseg
- 1) * sizeof(uint32_t) + 7) & -8;
1277 for (i
= (s1
->output_type
== TCC_OUTPUT_EXE
); i
< mo
->nseg
- 1; i
++) {
1278 int page_count
= (get_segment(mo
, i
)->vmsize
+ SEG_PAGE_SIZE
- 1) / SEG_PAGE_SIZE
;
1279 size
+= (sizeof(struct dyld_chained_starts_in_segment
) + (page_count
- 1) * sizeof(uint16_t) + 7) & -8;
1281 size
+= mo
->n_bind
* sizeof (struct dyld_chained_import
) + 1;
1282 for (i
= 0; i
< mo
->n_bind_rebase
; i
++) {
1283 if (mo
->bind_rebase
[i
].bind
) {
1284 int sym_index
= ELFW(R_SYM
)(mo
->bind_rebase
[i
].rel
.r_info
);
1285 ElfW(Sym
) *sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1286 const char *name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1287 size
+= strlen(name
) + 1;
1290 size
= (size
+ 7) & -8;
1291 section_ptr_add(mo
->chained_fixups
, size
);
1296 static void set_segment_and_offset(TCCState
*s1
, struct macho
*mo
, addr_t addr
,
1297 uint8_t *ptr
, int opcode
,
1298 Section
*sec
, addr_t offset
)
1301 struct segment_command_64
*seg
= NULL
;
1303 for (i
= (s1
->output_type
== TCC_OUTPUT_EXE
); i
< mo
->nseg
- 1; i
++) {
1304 seg
= get_segment(mo
, i
);
1305 if (addr
>= seg
->vmaddr
&& addr
< (seg
->vmaddr
+ seg
->vmsize
))
1309 write_uleb128(sec
, offset
- seg
->vmaddr
);
1312 static void bind_rebase(TCCState
*s1
, struct macho
*mo
)
1319 for (i
= 0; i
< mo
->n_lazy_bind
; i
++) {
1320 int sym_index
= ELFW(R_SYM
)(mo
->s_lazy_bind
[i
].rel
.r_info
);
1322 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1323 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1324 write32le(mo
->stub_helper
->data
+
1325 mo
->s_lazy_bind
[i
].bind_offset
,
1326 mo
->lazy_binding
->data_offset
);
1327 ptr
= section_ptr_add(mo
->lazy_binding
, 1);
1328 set_segment_and_offset(s1
, mo
, mo
->la_symbol_ptr
->sh_addr
, ptr
,
1329 BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
,
1331 mo
->s_lazy_bind
[i
].la_symbol_offset
+
1332 mo
->la_symbol_ptr
->sh_addr
);
1333 ptr
= section_ptr_add(mo
->lazy_binding
, 5 + strlen(name
));
1334 *ptr
++ = BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
|
1335 (BIND_SPECIAL_DYLIB_FLAT_LOOKUP
& 0xf);
1336 *ptr
++ = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
| 0;
1337 strcpy((char *)ptr
, name
);
1338 ptr
+= strlen(name
) + 1;
1339 *ptr
++ = BIND_OPCODE_DO_BIND
;
1340 *ptr
= BIND_OPCODE_DONE
;
1342 for (i
= 0; i
< mo
->n_rebase
; i
++) {
1343 Section
*s
= s1
->sections
[mo
->s_rebase
[i
].section
];
1345 ptr
= section_ptr_add(mo
->rebase
, 2);
1346 *ptr
++ = REBASE_OPCODE_SET_TYPE_IMM
| REBASE_TYPE_POINTER
;
1347 set_segment_and_offset(s1
, mo
, s
->sh_addr
, ptr
,
1348 REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
,
1350 mo
->s_rebase
[i
].rel
.r_offset
+
1352 ptr
= section_ptr_add(mo
->rebase
, 1);
1353 *ptr
= REBASE_OPCODE_DO_REBASE_IMM_TIMES
| 1;
1355 for (i
= 0; i
< mo
->n_bind
; i
++) {
1356 int sym_index
= ELFW(R_SYM
)(mo
->bind
[i
].rel
.r_info
);
1357 Section
*s
= s1
->sections
[mo
->bind
[i
].section
];
1360 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
1361 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
1362 binding
= ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
1363 ? mo
->weak_binding
: mo
->binding
;
1364 ptr
= section_ptr_add(binding
, 4 + (binding
== mo
->binding
) +
1366 if (binding
== mo
->binding
)
1367 *ptr
++ = BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
|
1368 (BIND_SPECIAL_DYLIB_FLAT_LOOKUP
& 0xf);
1369 *ptr
++ = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
|
1370 (binding
== mo
->weak_binding
1371 ? BIND_SYMBOL_FLAGS_WEAK_IMPORT
: 0);
1372 strcpy((char *)ptr
, name
);
1373 ptr
+= strlen(name
) + 1;
1374 *ptr
++ = BIND_OPCODE_SET_TYPE_IMM
| BIND_TYPE_POINTER
;
1375 set_segment_and_offset(s1
, mo
, s
->sh_addr
, ptr
,
1376 BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
,
1378 mo
->bind
[i
].rel
.r_offset
+ s
->sh_addr
);
1379 ptr
= section_ptr_add(binding
, 1);
1380 *ptr
++ = BIND_OPCODE_DO_BIND
;
1382 if (mo
->rebase
->data_offset
) {
1383 ptr
= section_ptr_add(mo
->rebase
, 1);
1384 *ptr
= REBASE_OPCODE_DONE
;
1386 if (mo
->binding
->data_offset
) {
1387 ptr
= section_ptr_add(mo
->binding
, 1);
1388 *ptr
= BIND_OPCODE_DONE
;
1390 if (mo
->weak_binding
->data_offset
) {
1391 ptr
= section_ptr_add(mo
->weak_binding
, 1);
1392 *ptr
= BIND_OPCODE_DONE
;
1394 tcc_free(mo
->s_lazy_bind
);
1395 tcc_free(mo
->s_rebase
);
1414 struct trie_node
*child
;
1419 struct trie_node
*node
;
1424 static void create_trie(struct trie_node
*node
,
1425 int from
, int to
, int index_start
,
1426 int n_trie
, struct trie_info
*trie
)
1429 int start
, end
, index_end
;
1431 struct trie_node
*child
;
1433 for (i
= from
; i
< to
; i
= end
) {
1434 cur
= trie
[i
].name
[index_start
];
1437 if (cur
!= trie
[i
].name
[index_start
])
1440 if (start
== end
- 1 ||
1441 (trie
[start
].name
[index_start
] &&
1442 trie
[start
].name
[index_start
+ 1] == 0))
1443 index_end
= trie
[start
].str_size
- 1;
1445 index_end
= index_start
+ 1;
1447 cur
= trie
[start
].name
[index_end
];
1448 for (i
= start
+ 1; i
< end
; i
++)
1449 if (cur
!= trie
[i
].name
[index_end
])
1451 if (trie
[start
].name
[index_end
] &&
1452 trie
[start
].name
[index_end
+ 1] == 0) {
1454 index_end
= trie
[start
].str_size
- 1;
1462 node
->child
= tcc_realloc(node
->child
,
1463 (node
->n_child
+ 1) *
1464 sizeof(struct trie_node
));
1465 child
= &node
->child
[node
->n_child
];
1466 child
->start
= start
;
1468 child
->index_start
= index_start
;
1469 child
->index_end
= index_end
;
1471 child
->child
= NULL
;
1473 if (start
!= end
- 1)
1474 create_trie(child
, start
, end
, index_end
, n_trie
, trie
);
1478 static int create_seq(int *offset
, int *n_seq
, struct trie_seq
**seq
,
1479 struct trie_node
*node
,
1480 int n_trie
, struct trie_info
*trie
)
1482 int i
, nest_offset
, last_seq
= *n_seq
, retval
= *offset
;
1483 struct trie_seq
*p_seq
;
1484 struct trie_node
*p_nest
;
1486 for (i
= 0; i
< node
->n_child
; i
++) {
1487 p_nest
= &node
->child
[i
];
1488 *seq
= tcc_realloc(*seq
, (*n_seq
+ 1) * sizeof(struct trie_seq
));
1489 p_seq
= &(*seq
)[(*n_seq
)++];
1490 p_seq
->n_child
= i
== 0 ? node
->n_child
: -1;
1491 p_seq
->node
= p_nest
;
1492 p_seq
->offset
= *offset
;
1493 p_seq
->nest_offset
= 0;
1494 *offset
+= (i
== 0 ? 1 + 1 : 0) +
1495 p_nest
->index_end
- p_nest
->index_start
+ 1 + 3;
1497 for (i
= 0; i
< node
->n_child
; i
++) {
1499 create_seq(offset
, n_seq
, seq
, &node
->child
[i
], n_trie
, trie
);
1500 p_seq
= &(*seq
)[last_seq
+ i
];
1501 p_seq
->nest_offset
= nest_offset
;
1506 static void node_free(struct trie_node
*node
)
1510 for (i
= 0; i
< node
->n_child
; i
++)
1511 node_free(&node
->child
[i
]);
1512 tcc_free(node
->child
);
1515 static int triecmp(const void *_a
, const void *_b
, void *arg
)
1517 struct trie_info
*a
= (struct trie_info
*) _a
;
1518 struct trie_info
*b
= (struct trie_info
*) _b
;
1519 int len_a
= strlen(a
->name
);
1520 int len_b
= strlen(b
->name
);
1522 /* strange sorting needed. Name 'xx' should be after 'xx1' */
1523 if (!strncmp(a
->name
, b
->name
, len_a
< len_b
? len_a
: len_b
))
1524 return len_a
< len_b
? 1 : (len_a
> len_b
? -1 : 0);
1525 return strcmp(a
->name
, b
->name
);
1528 static void export_trie(TCCState
*s1
, struct macho
*mo
)
1530 int i
, size
, offset
= 0, save_offset
;
1533 int sym_end
= symtab_section
->data_offset
/ sizeof(ElfW(Sym
));
1534 int n_trie
= 0, n_seq
= 0;
1535 struct trie_info
*trie
= NULL
, *p_trie
;
1536 struct trie_node node
, *p_node
;
1537 struct trie_seq
*seq
= NULL
;
1538 addr_t vm_addr
= get_segment(mo
, s1
->output_type
== TCC_OUTPUT_EXE
)->vmaddr
;
1540 for (sym_index
= 1; sym_index
< sym_end
; ++sym_index
) {
1541 ElfW(Sym
) *sym
= (ElfW(Sym
) *)symtab_section
->data
+ sym_index
;
1542 const char *name
= (char*)symtab_section
->link
->data
+ sym
->st_name
;
1544 if (sym
->st_shndx
!= SHN_UNDEF
&& sym
->st_shndx
< SHN_LORESERVE
&&
1545 (ELFW(ST_BIND
)(sym
->st_info
) == STB_GLOBAL
||
1546 ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
)) {
1547 int flag
= EXPORT_SYMBOL_FLAGS_KIND_REGULAR
;
1549 sym
->st_value
+ s1
->sections
[sym
->st_shndx
]->sh_addr
- vm_addr
;
1551 if (ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
)
1552 flag
|= EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
;
1553 dprintf ("%s %d %llx\n", name
, flag
, (long long)addr
+ vm_addr
);
1554 trie
= tcc_realloc(trie
, (n_trie
+ 1) * sizeof(struct trie_info
));
1555 trie
[n_trie
].name
= name
;
1556 trie
[n_trie
].flag
= flag
;
1557 trie
[n_trie
].addr
= addr
;
1558 trie
[n_trie
].str_size
= strlen(name
) + 1;
1559 trie
[n_trie
].term_size
= uleb128_size(flag
) + uleb128_size(addr
);
1564 tcc_qsort(trie
, n_trie
, sizeof(struct trie_info
), triecmp
, NULL
);
1565 memset(&node
, 0, sizeof(node
));
1566 create_trie(&node
, 0, n_trie
, 0, n_trie
, trie
);
1567 create_seq(&offset
, &n_seq
, &seq
, &node
, n_trie
, trie
);
1568 save_offset
= offset
;
1569 for (i
= 0; i
< n_seq
; i
++) {
1570 p_node
= seq
[i
].node
;
1571 if (p_node
->n_child
== 0) {
1572 p_trie
= &trie
[p_node
->start
];
1573 seq
[i
].nest_offset
= offset
;
1574 offset
+= 1 + p_trie
->term_size
+ 1;
1577 for (i
= 0; i
< n_seq
; i
++) {
1578 p_node
= seq
[i
].node
;
1579 p_trie
= &trie
[p_node
->start
];
1580 if (seq
[i
].n_child
>= 0) {
1581 section_ptr_add(mo
->exports
,
1582 seq
[i
].offset
- mo
->exports
->data_offset
);
1583 ptr
= section_ptr_add(mo
->exports
, 2);
1585 *ptr
= seq
[i
].n_child
;
1587 size
= p_node
->index_end
- p_node
->index_start
;
1588 ptr
= section_ptr_add(mo
->exports
, size
+ 1);
1589 memcpy(ptr
, &p_trie
->name
[p_node
->index_start
], size
);
1591 write_uleb128(mo
->exports
, seq
[i
].nest_offset
);
1593 section_ptr_add(mo
->exports
, save_offset
- mo
->exports
->data_offset
);
1594 for (i
= 0; i
< n_seq
; i
++) {
1595 p_node
= seq
[i
].node
;
1596 if (p_node
->n_child
== 0) {
1597 p_trie
= &trie
[p_node
->start
];
1598 write_uleb128(mo
->exports
, p_trie
->term_size
);
1599 write_uleb128(mo
->exports
, p_trie
->flag
);
1600 write_uleb128(mo
->exports
, p_trie
->addr
);
1601 ptr
= section_ptr_add(mo
->exports
, 1);
1605 section_ptr_add(mo
->exports
, -mo
->exports
->data_offset
& 7);
1612 static void collect_sections(TCCState
*s1
, struct macho
*mo
, const char *filename
)
1615 int used_segment
[N_SEGMENT
];
1616 uint64_t curaddr
, fileofs
;
1618 struct segment_command_64
*seg
;
1619 struct dylib_command
*dylib
;
1620 #ifdef CONFIG_NEW_MACHO
1621 struct linkedit_data_command
*chained_fixups_lc
;
1622 struct linkedit_data_command
*export_trie_lc
;
1624 struct build_version_command
*dyldbv
;
1625 struct source_version_command
*dyldsv
;
1626 struct rpath_command
*rpath
;
1627 struct dylinker_command
*dyldlc
;
1628 struct symtab_command
*symlc
;
1629 struct dysymtab_command
*dysymlc
;
1632 for (i
= 0; i
< N_SEGMENT
; i
++)
1633 used_segment
[i
] = all_segment
[i
].used
;
1635 memset (mo
->sk_to_sect
, 0, sizeof(mo
->sk_to_sect
));
1636 for (i
= s1
->nb_sections
; i
-- > 1;) {
1638 s
= s1
->sections
[i
];
1640 flags
= s
->sh_flags
;
1642 /* debug sections have sometimes no SHF_ALLOC */
1643 if ((flags
& SHF_ALLOC
) || !strncmp(s
->name
, ".debug_", 7)) {
1645 default: sk
= sk_unknown
; break;
1646 case SHT_INIT_ARRAY
: sk
= sk_init
; break;
1647 case SHT_FINI_ARRAY
: sk
= sk_fini
; break;
1648 case SHT_NOBITS
: sk
= sk_bss
; break;
1649 case SHT_SYMTAB
: sk
= sk_discard
; break;
1651 if (s
== stabstr_section
)
1656 case SHT_RELX
: sk
= sk_discard
; break;
1657 case SHT_LINKEDIT
: sk
= sk_linkedit
; break;
1661 #ifndef CONFIG_NEW_MACHO
1662 else if (s
== mo
->stub_helper
)
1663 sk
= sk_stub_helper
;
1664 else if (s
== mo
->la_symbol_ptr
)
1667 else if (s
== rodata_section
)
1669 else if (s
== s1
->got
)
1671 else if (s
== stab_section
)
1673 else if (s
== dwarf_info_section
)
1675 else if (s
== dwarf_abbrev_section
)
1676 sk
= sk_debug_abbrev
;
1677 else if (s
== dwarf_line_section
)
1679 else if (s
== dwarf_aranges_section
)
1680 sk
= sk_debug_aranges
;
1681 else if (s
== dwarf_str_section
)
1683 else if (s
== dwarf_line_str_section
)
1684 sk
= sk_debug_line_str
;
1685 else if (flags
& SHF_EXECINSTR
)
1687 else if (flags
& SHF_WRITE
)
1695 s
->prev
= mo
->sk_to_sect
[sk
].s
;
1696 mo
->sk_to_sect
[sk
].s
= s
;
1697 used_segment
[skinfo
[sk
].seg_initial
] = 1;
1700 if (s1
->output_type
!= TCC_OUTPUT_EXE
)
1701 used_segment
[0] = 0;
1703 for (i
= 0; i
< N_SEGMENT
; i
++)
1704 if (used_segment
[i
]) {
1705 seg
= add_segment(mo
, all_segment
[i
].name
);
1706 if (i
== 1 && s1
->output_type
!= TCC_OUTPUT_EXE
)
1709 seg
->vmaddr
= all_segment
[i
].vmaddr
;
1710 seg
->vmsize
= all_segment
[i
].vmsize
;
1711 seg
->maxprot
= all_segment
[i
].maxprot
;
1712 seg
->initprot
= all_segment
[i
].initprot
;
1713 seg
->flags
= all_segment
[i
].flags
;
1714 for (sk
= sk_unknown
; sk
< sk_last
; sk
++)
1715 if (skinfo
[sk
].seg_initial
== i
)
1716 mo
->segment
[sk
] = mo
->nseg
- 1;
1719 if (s1
->output_type
!= TCC_OUTPUT_EXE
) {
1720 const char *name
= s1
->install_name
? s1
->install_name
: filename
;
1721 i
= (sizeof(*dylib
) + strlen(name
) + 1 + 7) &-8;
1722 dylib
= add_lc(mo
, LC_ID_DYLIB
, i
);
1723 dylib
->name
= sizeof(*dylib
);
1724 dylib
->timestamp
= 1;
1725 dylib
->current_version
=
1726 s1
->current_version
? s1
->current_version
: 1 << 16;
1727 dylib
->compatibility_version
=
1728 s1
->compatibility_version
? s1
->compatibility_version
: 1 << 16;
1729 str
= (char*)dylib
+ dylib
->name
;
1733 #ifdef CONFIG_NEW_MACHO
1734 chained_fixups_lc
= add_lc(mo
, LC_DYLD_CHAINED_FIXUPS
,
1735 sizeof(struct linkedit_data_command
));
1736 export_trie_lc
= add_lc(mo
, LC_DYLD_EXPORTS_TRIE
,
1737 sizeof(struct linkedit_data_command
));
1739 mo
->dyldinfo
= add_lc(mo
, LC_DYLD_INFO_ONLY
, sizeof(*mo
->dyldinfo
));
1742 symlc
= add_lc(mo
, LC_SYMTAB
, sizeof(*symlc
));
1743 dysymlc
= add_lc(mo
, LC_DYSYMTAB
, sizeof(*dysymlc
));
1745 if (s1
->output_type
== TCC_OUTPUT_EXE
) {
1746 i
= (sizeof(*dyldlc
) + strlen("/usr/lib/dyld") + 1 + 7) &-8;
1747 dyldlc
= add_lc(mo
, LC_LOAD_DYLINKER
, i
);
1748 dyldlc
->name
= sizeof(*dyldlc
);
1749 str
= (char*)dyldlc
+ dyldlc
->name
;
1750 strcpy(str
, "/usr/lib/dyld");
1753 dyldbv
= add_lc(mo
, LC_BUILD_VERSION
, sizeof(*dyldbv
));
1754 dyldbv
->platform
= PLATFORM_MACOS
;
1755 dyldbv
->minos
= (10 << 16) + (6 << 8);
1756 dyldbv
->sdk
= (10 << 16) + (6 << 8);
1759 dyldsv
= add_lc(mo
, LC_SOURCE_VERSION
, sizeof(*dyldsv
));
1760 dyldsv
->version
= 0;
1762 if (s1
->output_type
== TCC_OUTPUT_EXE
) {
1763 mo
->ep
= add_lc(mo
, LC_MAIN
, sizeof(*mo
->ep
));
1764 mo
->ep
->entryoff
= 4096;
1767 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++) {
1768 DLLReference
*dllref
= s1
->loaded_dlls
[i
];
1769 if (dllref
->level
== 0)
1770 add_dylib(mo
, dllref
->name
);
1774 char *path
= s1
->rpath
, *end
;
1776 end
= strchr(path
, ':');
1778 end
= strchr(path
, 0);
1779 i
= (sizeof(*rpath
) + (end
- path
) + 1 + 7) &-8;
1780 rpath
= add_lc(mo
, LC_RPATH
, i
);
1781 rpath
->path
= sizeof(*rpath
);
1782 str
= (char*)rpath
+ rpath
->path
;
1783 memcpy(str
, path
, end
- path
);
1784 str
[end
- path
] = 0;
1789 fileofs
= 4096; /* leave space for mach-o headers */
1790 curaddr
= get_segment(mo
, s1
->output_type
== TCC_OUTPUT_EXE
)->vmaddr
;
1794 mo
->elfsectomacho
= tcc_mallocz(sizeof(*mo
->elfsectomacho
) * s1
->nb_sections
);
1795 for (sk
= sk_unknown
; sk
< sk_last
; sk
++) {
1796 struct section_64
*sec
= NULL
;
1798 seg
->vmsize
= curaddr
- seg
->vmaddr
;
1799 seg
->filesize
= fileofs
- seg
->fileoff
;
1801 #ifdef CONFIG_NEW_MACHO
1802 if (sk
== sk_linkedit
) {
1803 calc_fixup_size(s1
, mo
);
1804 export_trie(s1
, mo
);
1807 if (sk
== sk_linkedit
) {
1808 bind_rebase(s1
, mo
);
1809 export_trie(s1
, mo
);
1812 if (skinfo
[sk
].seg_initial
&&
1813 (s1
->output_type
!= TCC_OUTPUT_EXE
|| mo
->segment
[sk
]) &&
1814 mo
->sk_to_sect
[sk
].s
) {
1817 seg
= get_segment(mo
, mo
->segment
[sk
]);
1818 if (skinfo
[sk
].name
) {
1819 si
= add_section(mo
, &seg
, skinfo
[sk
].name
);
1821 mo
->lc
[mo
->seg2lc
[mo
->segment
[sk
]]] = (struct load_command
*)seg
;
1822 mo
->sk_to_sect
[sk
].machosect
= si
;
1823 sec
= get_section(seg
, si
);
1824 sec
->flags
= skinfo
[sk
].flags
;
1826 #ifdef TCC_TARGET_X86_64
1828 #elif defined TCC_TARGET_ARM64
1829 sec
->reserved2
= 12;
1831 if (sk
== sk_nl_ptr
)
1832 sec
->reserved1
= mo
->nr_plt
;
1833 #ifndef CONFIG_NEW_MACHO
1834 if (sk
== sk_la_ptr
)
1835 sec
->reserved1
= mo
->nr_plt
+ mo
->n_got
;
1838 if (seg
->vmaddr
== -1) {
1839 curaddr
= (curaddr
+ SEG_PAGE_SIZE
- 1) & -SEG_PAGE_SIZE
;
1840 seg
->vmaddr
= curaddr
;
1841 fileofs
= (fileofs
+ SEG_PAGE_SIZE
- 1) & -SEG_PAGE_SIZE
;
1842 seg
->fileoff
= fileofs
;
1845 for (s
= mo
->sk_to_sect
[sk
].s
; s
; s
= s
->prev
) {
1846 int a
= exact_log2p1(s
->sh_addralign
);
1847 if (a
&& al
< (a
- 1))
1849 s
->sh_size
= s
->data_offset
;
1855 tcc_warning("alignment > 4096"), sec
->align
= 12, al
= 4096;
1856 curaddr
= (curaddr
+ al
- 1) & -al
;
1857 fileofs
= (fileofs
+ al
- 1) & -al
;
1859 sec
->addr
= curaddr
;
1860 sec
->offset
= fileofs
;
1862 for (s
= mo
->sk_to_sect
[sk
].s
; s
; s
= s
->prev
) {
1863 al
= s
->sh_addralign
;
1864 curaddr
= (curaddr
+ al
- 1) & -al
;
1865 dprintf("%s: curaddr now 0x%lx\n", s
->name
, (long)curaddr
);
1866 s
->sh_addr
= curaddr
;
1867 curaddr
+= s
->sh_size
;
1868 if (s
->sh_type
!= SHT_NOBITS
) {
1869 fileofs
= (fileofs
+ al
- 1) & -al
;
1870 s
->sh_offset
= fileofs
;
1871 fileofs
+= s
->sh_size
;
1872 dprintf("%s: fileofs now %ld\n", s
->name
, (long)fileofs
);
1875 mo
->elfsectomacho
[s
->sh_num
] = numsec
;
1878 sec
->size
= curaddr
- sec
->addr
;
1881 for (s
= mo
->sk_to_sect
[sk
].s
; s
; s
= s
->prev
) {
1882 int type
= s
->sh_type
;
1883 int flags
= s
->sh_flags
;
1884 printf("%d section %-16s %-10s %09lx %04x %02d %s,%s,%s\n",
1887 type
== SHT_PROGBITS
? "progbits" :
1888 type
== SHT_NOBITS
? "nobits" :
1889 type
== SHT_SYMTAB
? "symtab" :
1890 type
== SHT_STRTAB
? "strtab" :
1891 type
== SHT_INIT_ARRAY
? "init" :
1892 type
== SHT_FINI_ARRAY
? "fini" :
1893 type
== SHT_RELX
? "rel" : "???",
1895 (unsigned)s
->data_offset
,
1897 flags
& SHF_ALLOC
? "alloc" : "",
1898 flags
& SHF_WRITE
? "write" : "",
1899 flags
& SHF_EXECINSTR
? "exec" : ""
1904 seg
->vmsize
= curaddr
- seg
->vmaddr
;
1905 seg
->filesize
= fileofs
- seg
->fileoff
;
1908 /* Fill symtab info */
1909 symlc
->symoff
= mo
->symtab
->sh_offset
;
1910 symlc
->nsyms
= mo
->symtab
->data_offset
/ sizeof(struct nlist_64
);
1911 symlc
->stroff
= mo
->strtab
->sh_offset
;
1912 symlc
->strsize
= mo
->strtab
->data_offset
;
1914 dysymlc
->iundefsym
= mo
->iundef
== -1 ? symlc
->nsyms
: mo
->iundef
;
1915 dysymlc
->iextdefsym
= mo
->iextdef
== -1 ? dysymlc
->iundefsym
: mo
->iextdef
;
1916 dysymlc
->ilocalsym
= mo
->ilocal
== -1 ? dysymlc
->iextdefsym
: mo
->ilocal
;
1917 dysymlc
->nlocalsym
= dysymlc
->iextdefsym
- dysymlc
->ilocalsym
;
1918 dysymlc
->nextdefsym
= dysymlc
->iundefsym
- dysymlc
->iextdefsym
;
1919 dysymlc
->nundefsym
= symlc
->nsyms
- dysymlc
->iundefsym
;
1920 dysymlc
->indirectsymoff
= mo
->indirsyms
->sh_offset
;
1921 dysymlc
->nindirectsyms
= mo
->indirsyms
->data_offset
/ sizeof(uint32_t);
1923 #ifdef CONFIG_NEW_MACHO
1924 if (mo
->chained_fixups
->data_offset
) {
1925 chained_fixups_lc
->dataoff
= mo
->chained_fixups
->sh_offset
;
1926 chained_fixups_lc
->datasize
= mo
->chained_fixups
->data_offset
;
1928 if (mo
->exports
->data_offset
) {
1929 export_trie_lc
->dataoff
= mo
->exports
->sh_offset
;
1930 export_trie_lc
->datasize
= mo
->exports
->data_offset
;
1933 if (mo
->rebase
->data_offset
) {
1934 mo
->dyldinfo
->rebase_off
= mo
->rebase
->sh_offset
;
1935 mo
->dyldinfo
->rebase_size
= mo
->rebase
->data_offset
;
1937 if (mo
->binding
->data_offset
) {
1938 mo
->dyldinfo
->bind_off
= mo
->binding
->sh_offset
;
1939 mo
->dyldinfo
->bind_size
= mo
->binding
->data_offset
;
1941 if (mo
->weak_binding
->data_offset
) {
1942 mo
->dyldinfo
->weak_bind_off
= mo
->weak_binding
->sh_offset
;
1943 mo
->dyldinfo
->weak_bind_size
= mo
->weak_binding
->data_offset
;
1945 if (mo
->lazy_binding
->data_offset
) {
1946 mo
->dyldinfo
->lazy_bind_off
= mo
->lazy_binding
->sh_offset
;
1947 mo
->dyldinfo
->lazy_bind_size
= mo
->lazy_binding
->data_offset
;
1949 if (mo
->exports
->data_offset
) {
1950 mo
->dyldinfo
->export_off
= mo
->exports
->sh_offset
;
1951 mo
->dyldinfo
->export_size
= mo
->exports
->data_offset
;
1956 static void macho_write(TCCState
*s1
, struct macho
*mo
, FILE *fp
)
1959 uint64_t fileofs
= 0;
1961 mo
->mh
.mh
.magic
= MH_MAGIC_64
;
1962 #ifdef TCC_TARGET_X86_64
1963 mo
->mh
.mh
.cputype
= CPU_TYPE_X86_64
;
1964 mo
->mh
.mh
.cpusubtype
= CPU_SUBTYPE_LIB64
| CPU_SUBTYPE_X86_ALL
;
1965 #elif defined TCC_TARGET_ARM64
1966 mo
->mh
.mh
.cputype
= CPU_TYPE_ARM64
;
1967 mo
->mh
.mh
.cpusubtype
= CPU_SUBTYPE_ARM64_ALL
;
1969 if (s1
->output_type
== TCC_OUTPUT_EXE
) {
1970 mo
->mh
.mh
.filetype
= MH_EXECUTE
;
1971 mo
->mh
.mh
.flags
= MH_DYLDLINK
| MH_PIE
;
1974 mo
->mh
.mh
.filetype
= MH_DYLIB
;
1975 mo
->mh
.mh
.flags
= MH_DYLDLINK
;
1977 mo
->mh
.mh
.ncmds
= mo
->nlc
;
1978 mo
->mh
.mh
.sizeofcmds
= 0;
1979 for (i
= 0; i
< mo
->nlc
; i
++)
1980 mo
->mh
.mh
.sizeofcmds
+= mo
->lc
[i
]->cmdsize
;
1982 fwrite(&mo
->mh
, 1, sizeof(mo
->mh
), fp
);
1983 fileofs
+= sizeof(mo
->mh
);
1984 for (i
= 0; i
< mo
->nlc
; i
++) {
1985 fwrite(mo
->lc
[i
], 1, mo
->lc
[i
]->cmdsize
, fp
);
1986 fileofs
+= mo
->lc
[i
]->cmdsize
;
1989 for (sk
= sk_unknown
; sk
< sk_last
; sk
++) {
1990 //struct segment_command_64 *seg;
1991 if (skinfo
[sk
].seg_initial
== 0 ||
1992 (s1
->output_type
== TCC_OUTPUT_EXE
&& !mo
->segment
[sk
]) ||
1993 !mo
->sk_to_sect
[sk
].s
)
1995 /*seg =*/ get_segment(mo
, mo
->segment
[sk
]);
1996 for (s
= mo
->sk_to_sect
[sk
].s
; s
; s
= s
->prev
) {
1997 if (s
->sh_type
!= SHT_NOBITS
) {
1998 while (fileofs
< s
->sh_offset
)
1999 fputc(0, fp
), fileofs
++;
2001 fwrite(s
->data
, 1, s
->sh_size
, fp
);
2002 fileofs
+= s
->sh_size
;
2009 #ifdef CONFIG_NEW_MACHO
2010 static int bind_rebase_cmp(const void *_a
, const void *_b
, void *arg
)
2013 struct bind_rebase
*a
= (struct bind_rebase
*) _a
;
2014 struct bind_rebase
*b
= (struct bind_rebase
*) _b
;
2015 addr_t aa
= s1
->sections
[a
->section
]->sh_addr
+ a
->rel
.r_offset
;
2016 addr_t ab
= s1
->sections
[b
->section
]->sh_addr
+ b
->rel
.r_offset
;
2018 return aa
> ab
? 1 : aa
< ab
? -1 : 0;
2021 ST_FUNC
void bind_rebase_import(TCCState
*s1
, struct macho
*mo
)
2023 int i
, j
, k
, bind_index
, size
, page_count
, sym_index
;
2026 unsigned char *data
= mo
->chained_fixups
->data
;
2027 struct segment_command_64
*seg
;
2028 struct dyld_chained_fixups_header
*header
;
2029 struct dyld_chained_starts_in_image
*image
;
2030 struct dyld_chained_starts_in_segment
*segment
;
2031 struct dyld_chained_import
*import
;
2033 tcc_qsort(mo
->bind_rebase
, mo
->n_bind_rebase
, sizeof(struct bind_rebase
),
2034 bind_rebase_cmp
, s1
);
2035 for (i
= 0; i
< mo
->n_bind_rebase
- 1; i
++)
2036 if (mo
->bind_rebase
[i
].section
== mo
->bind_rebase
[i
+ 1].section
&&
2037 mo
->bind_rebase
[i
].rel
.r_offset
== mo
->bind_rebase
[i
+ 1].rel
.r_offset
) {
2038 sym_index
= ELFW(R_SYM
)(mo
->bind_rebase
[i
].rel
.r_info
);
2039 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
2040 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
2041 tcc_error("Overlap %s/%s %s:%s",
2042 mo
->bind_rebase
[i
].bind
? "bind" : "rebase",
2043 mo
->bind_rebase
[i
+ 1].bind
? "bind" : "rebase",
2044 s1
->sections
[mo
->bind_rebase
[i
].section
]->name
, name
);
2046 header
= (struct dyld_chained_fixups_header
*) data
;
2047 data
+= (sizeof(struct dyld_chained_fixups_header
) + 7) & -8;
2048 header
->starts_offset
= data
- mo
->chained_fixups
->data
;
2049 header
->imports_count
= mo
->n_bind
;
2050 header
->imports_format
= DYLD_CHAINED_IMPORT
;
2051 header
->symbols_format
= 0;
2052 size
= sizeof(struct dyld_chained_starts_in_image
) +
2053 (mo
->nseg
- 1) * sizeof(uint32_t);
2054 image
= (struct dyld_chained_starts_in_image
*) data
;
2055 data
+= (size
+ 7) & -8;
2056 image
->seg_count
= mo
->nseg
;
2057 for (i
= (s1
->output_type
== TCC_OUTPUT_EXE
); i
< mo
->nseg
- 1; i
++) {
2058 image
->seg_info_offset
[i
] = (data
- mo
->chained_fixups
->data
) -
2059 header
->starts_offset
;
2060 seg
= get_segment(mo
, i
);
2061 page_count
= (seg
->vmsize
+ SEG_PAGE_SIZE
- 1) / SEG_PAGE_SIZE
;
2062 size
= sizeof(struct dyld_chained_starts_in_segment
) +
2063 (page_count
- 1) * sizeof(uint16_t);
2064 segment
= (struct dyld_chained_starts_in_segment
*) data
;
2065 data
+= (size
+ 7) & -8;
2066 segment
->size
= size
;
2067 segment
->page_size
= SEG_PAGE_SIZE
;
2069 #define PTR_64_OFFSET 0
2070 #define PTR_64_MASK 0x7FFFFFFFFFFULL
2071 segment
->pointer_format
= DYLD_CHAINED_PTR_64
;
2073 #define PTR_64_OFFSET 0x100000000ULL
2074 #define PTR_64_MASK 0xFFFFFFFFFFFFFFULL
2075 segment
->pointer_format
= DYLD_CHAINED_PTR_64_OFFSET
;
2077 segment
->segment_offset
= seg
->fileoff
;
2078 segment
->max_valid_pointer
= 0;
2079 segment
->page_count
= page_count
;
2083 for (j
= 0; j
< page_count
; j
++) {
2084 addr_t start
= seg
->vmaddr
+ j
* SEG_PAGE_SIZE
;
2085 addr_t end
= start
+ SEG_PAGE_SIZE
;
2089 struct dyld_chained_ptr_64_rebase
*rebase
;
2090 struct dyld_chained_ptr_64_bind
*bind
;
2092 segment
->page_start
[j
] = DYLD_CHAINED_PTR_START_NONE
;
2093 for (; k
< mo
->n_bind_rebase
; k
++) {
2094 Section
*s
= s1
->sections
[mo
->bind_rebase
[k
].section
];
2095 addr_t r_offset
= mo
->bind_rebase
[k
].rel
.r_offset
;
2096 addr_t addr
= s
->sh_addr
+ r_offset
;
2099 (addr
& (SEG_PAGE_SIZE
- 1)) > SEG_PAGE_SIZE
- PTR_SIZE
)
2100 tcc_error("Illegal rel_offset %s %lld",
2101 s
->name
, (long long)r_offset
);
2104 if (addr
>= start
) {
2105 cur_o
= addr
- start
;
2106 if (mo
->bind_rebase
[k
].bind
) {
2107 if (segment
->page_start
[j
] == DYLD_CHAINED_PTR_START_NONE
)
2108 segment
->page_start
[j
] = cur_o
;
2110 bind
= (struct dyld_chained_ptr_64_bind
*) last
;
2111 bind
->next
= (cur_o
- last_o
) / 4;
2113 bind
= (struct dyld_chained_ptr_64_bind
*)
2114 (s
->data
+ r_offset
);
2117 bind
->ordinal
= bind_index
;
2124 if (segment
->page_start
[j
] == DYLD_CHAINED_PTR_START_NONE
)
2125 segment
->page_start
[j
] = cur_o
;
2127 rebase
= (struct dyld_chained_ptr_64_rebase
*) last
;
2128 rebase
->next
= (cur_o
- last_o
) / 4;
2130 rebase
= (struct dyld_chained_ptr_64_rebase
*)
2131 (s
->data
+ r_offset
);
2134 cur
= (*(uint64_t *) (s
->data
+ r_offset
)) -
2136 rebase
->target
= cur
& PTR_64_MASK
;
2137 rebase
->high8
= cur
>> (64 - 8);
2138 if (cur
!= ((uint64_t)rebase
->high8
<< (64 - 8)) + rebase
->target
)
2139 tcc_error("rebase error");
2140 rebase
->reserved
= 0;
2145 bind_index
+= mo
->bind_rebase
[k
].bind
;
2150 header
->imports_offset
= data
- mo
->chained_fixups
->data
;
2151 import
= (struct dyld_chained_import
*) data
;
2152 data
+= mo
->n_bind
* sizeof (struct dyld_chained_import
);
2153 header
->symbols_offset
= data
- mo
->chained_fixups
->data
;
2155 for (i
= 0, bind_index
= 0; i
< mo
->n_bind_rebase
; i
++) {
2156 if (mo
->bind_rebase
[i
].bind
) {
2157 import
[bind_index
].lib_ordinal
=
2158 BIND_SPECIAL_DYLIB_FLAT_LOOKUP
& 0xffu
;
2159 import
[bind_index
].name_offset
=
2160 (data
- mo
->chained_fixups
->data
) - header
->symbols_offset
;
2161 sym_index
= ELFW(R_SYM
)(mo
->bind_rebase
[i
].rel
.r_info
);
2162 sym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym_index
];
2163 import
[bind_index
].weak_import
=
2164 ELFW(ST_BIND
)(sym
->st_info
) == STB_WEAK
;
2165 name
= (char *) symtab_section
->link
->data
+ sym
->st_name
;
2166 strcpy((char *) data
, name
);
2167 data
+= strlen(name
) + 1;
2171 tcc_free(mo
->bind_rebase
);
2175 ST_FUNC
int macho_output_file(TCCState
*s1
, const char *filename
)
2177 int fd
, mode
, file_type
;
2182 (void)memset(&mo
, 0, sizeof(mo
));
2184 file_type
= s1
->output_type
;
2185 if (file_type
== TCC_OUTPUT_OBJ
)
2190 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, mode
);
2191 if (fd
< 0 || (fp
= fdopen(fd
, "wb")) == NULL
) {
2192 tcc_error_noabort("could not write '%s: %s'", filename
, strerror(errno
));
2196 printf("<- %s\n", filename
);
2198 tcc_add_runtime(s1
);
2199 tcc_macho_add_destructor(s1
);
2200 resolve_common_syms(s1
);
2201 create_symtab(s1
, &mo
);
2202 check_relocs(s1
, &mo
);
2203 ret
= check_symbols(s1
, &mo
);
2205 int save_output
= s1
->output_type
;
2207 collect_sections(s1
, &mo
, filename
);
2208 relocate_syms(s1
, s1
->symtab
, 0);
2209 if (s1
->output_type
== TCC_OUTPUT_EXE
)
2210 mo
.ep
->entryoff
= get_sym_addr(s1
, "main", 1, 1)
2211 - get_segment(&mo
, 1)->vmaddr
;
2214 // Macho uses bind/rebase instead of dynsym
2215 s1
->output_type
= TCC_OUTPUT_EXE
;
2216 relocate_sections(s1
);
2217 s1
->output_type
= save_output
;
2218 #ifdef CONFIG_NEW_MACHO
2219 bind_rebase_import(s1
, &mo
);
2221 convert_symbols(s1
, &mo
);
2222 macho_write(s1
, &mo
, fp
);
2226 for (i
= 0; i
< mo
.nlc
; i
++)
2228 tcc_free(mo
.seg2lc
);
2230 tcc_free(mo
.elfsectomacho
);
2231 tcc_free(mo
.e2msym
);
2234 #ifdef CONFIG_CODESIGN
2239 snprintf(command
, sizeof(command
), "codesign -f -s - %s", filename
);
2240 retval
= system (command
);
2241 if (retval
== -1 || !(WIFEXITED(retval
) && WEXITSTATUS(retval
) == 0))
2242 tcc_error ("command failed '%s'", command
);
2248 static uint32_t macho_swap32(uint32_t x
)
2250 return (x
>> 24) | (x
<< 24) | ((x
>> 8) & 0xff00) | ((x
& 0xff00) << 8);
2252 #define SWAP(x) (swap ? macho_swap32(x) : (x))
2253 #define tbd_parse_movepast(s) \
2254 (pos = (pos = strstr(pos, s)) ? pos + strlen(s) : NULL)
2255 #define tbd_parse_movetoany(cs) (pos = strpbrk(pos, cs))
2256 #define tbd_parse_skipws while (*pos && (*pos==' '||*pos=='\n')) ++pos
2257 #define tbd_parse_tramplequote if(*pos=='\''||*pos=='"') tbd_parse_trample
2258 #define tbd_parse_tramplespace if(*pos==' ') tbd_parse_trample
2259 #define tbd_parse_trample *pos++=0
2261 #ifdef TCC_IS_NATIVE
2262 /* Looks for the active developer SDK set by xcode-select (or the default
2263 one set during installation.) */
2264 ST_FUNC
void tcc_add_macos_sdkpath(TCCState
* s
)
2266 char *sdkroot
= NULL
, *pos
= NULL
;
2267 void* xcs
= dlopen("libxcselect.dylib", RTLD_GLOBAL
| RTLD_LAZY
);
2269 int (*f
)(unsigned int, char**) = dlsym(xcs
, "xcselect_host_sdk_path");
2271 if (f
) f(1, &sdkroot
);
2273 pos
= strstr(sdkroot
,"SDKs/MacOSX");
2275 cstr_printf(&path
, "%.*s.sdk/usr/lib", (int)(pos
- sdkroot
+ 11), sdkroot
);
2276 /* must use free from libc directly */
2277 #pragma push_macro("free")
2280 #pragma pop_macro("free")
2282 tcc_add_library_path(s
, (char*)path
.data
);
2284 tcc_add_library_path(s
,
2285 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
2286 ":" "/Applications/Xcode.app/Developer/SDKs/MacOSX.sdk/usr/lib"
2291 ST_FUNC
const char* macho_tbd_soname(const char* filename
) {
2292 char *soname
, *data
, *pos
;
2293 const char *ret
= filename
;
2295 int fd
= open(filename
,O_RDONLY
);
2296 if (fd
<0) return ret
;
2297 pos
= data
= tcc_load_text(fd
);
2298 if (!tbd_parse_movepast("install-name: ")) goto the_end
;
2300 tbd_parse_tramplequote
;
2302 if (!tbd_parse_movetoany("\n \"'")) goto the_end
;
2304 ret
= tcc_strdup(soname
);
2309 #endif /* TCC_IS_NATIVE */
2311 ST_FUNC
int macho_load_tbd(TCCState
* s1
, int fd
, const char* filename
, int lev
)
2313 char *soname
, *data
, *pos
;
2316 pos
= data
= tcc_load_text(fd
);
2317 if (!tbd_parse_movepast("install-name: ")) goto the_end
;
2319 tbd_parse_tramplequote
;
2321 if (!tbd_parse_movetoany("\n \"'")) goto the_end
;
2324 if (tcc_add_dllref(s1
, soname
, lev
)->found
)
2329 if (!tbd_parse_movepast("symbols: ")) break;
2330 if (!tbd_parse_movepast("[")) break;
2333 tbd_parse_tramplequote
;
2335 if (!tbd_parse_movetoany(",] \"'")) break;
2336 tbd_parse_tramplequote
;
2337 tbd_parse_tramplespace
;
2339 if (*pos
==0||*pos
==']') cont
=0;
2341 set_elf_sym(s1
->dynsymtab_section
, 0, 0,
2342 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0, SHN_UNDEF
, sym
);
2351 ST_FUNC
int macho_load_dll(TCCState
* s1
, int fd
, const char* filename
, int lev
)
2353 unsigned char buf
[sizeof(struct mach_header_64
)];
2355 uint32_t machofs
= 0;
2356 struct fat_header fh
;
2357 struct mach_header mh
;
2358 struct load_command
*lc
;
2360 const char *soname
= filename
;
2361 struct nlist_64
*symtab
= 0;
2364 uint32_t strsize
= 0;
2365 uint32_t iextdef
= 0;
2366 uint32_t nextdef
= 0;
2369 if (full_read(fd
, buf
, sizeof(buf
)) != sizeof(buf
))
2371 memcpy(&fh
, buf
, sizeof(fh
));
2372 if (fh
.magic
== FAT_MAGIC
|| fh
.magic
== FAT_CIGAM
) {
2373 struct fat_arch
*fa
= load_data(fd
, sizeof(fh
),
2374 fh
.nfat_arch
* sizeof(*fa
));
2375 swap
= fh
.magic
== FAT_CIGAM
;
2376 for (i
= 0; i
< SWAP(fh
.nfat_arch
); i
++)
2377 #ifdef TCC_TARGET_X86_64
2378 if (SWAP(fa
[i
].cputype
) == CPU_TYPE_X86_64
2379 && SWAP(fa
[i
].cpusubtype
) == CPU_SUBTYPE_X86_ALL
)
2380 #elif defined TCC_TARGET_ARM64
2381 if (SWAP(fa
[i
].cputype
) == CPU_TYPE_ARM64
2382 && SWAP(fa
[i
].cpusubtype
) == CPU_SUBTYPE_ARM64_ALL
)
2385 if (i
== SWAP(fh
.nfat_arch
)) {
2389 machofs
= SWAP(fa
[i
].offset
);
2391 lseek(fd
, machofs
, SEEK_SET
);
2393 } else if (fh
.magic
== FAT_MAGIC_64
|| fh
.magic
== FAT_CIGAM_64
) {
2394 tcc_warning("%s: Mach-O fat 64bit files of type 0x%x not handled",
2395 filename
, fh
.magic
);
2399 memcpy(&mh
, buf
, sizeof(mh
));
2400 if (mh
.magic
!= MH_MAGIC_64
)
2402 dprintf("found Mach-O at %d\n", machofs
);
2403 buf2
= load_data(fd
, machofs
+ sizeof(struct mach_header_64
), mh
.sizeofcmds
);
2404 for (i
= 0, lc
= buf2
; i
< mh
.ncmds
; i
++) {
2405 dprintf("lc %2d: 0x%08x\n", i
, lc
->cmd
);
2409 struct symtab_command
*sc
= (struct symtab_command
*)lc
;
2411 symtab
= load_data(fd
, machofs
+ sc
->symoff
, nsyms
* sizeof(*symtab
));
2412 strsize
= sc
->strsize
;
2413 strtab
= load_data(fd
, machofs
+ sc
->stroff
, strsize
);
2418 struct dylib_command
*dc
= (struct dylib_command
*)lc
;
2419 soname
= (char*)lc
+ dc
->name
;
2420 dprintf(" ID_DYLIB %d 0x%x 0x%x %s\n",
2421 dc
->timestamp
, dc
->current_version
,
2422 dc
->compatibility_version
, soname
);
2425 case LC_REEXPORT_DYLIB
:
2427 struct dylib_command
*dc
= (struct dylib_command
*)lc
;
2428 char *name
= (char*)lc
+ dc
->name
;
2429 int subfd
= open(name
, O_RDONLY
| O_BINARY
);
2430 dprintf(" REEXPORT %s\n", name
);
2432 tcc_warning("can't open %s (reexported from %s)", name
, filename
);
2434 /* Hopefully the REEXPORTs never form a cycle, we don't check
2436 macho_load_dll(s1
, subfd
, name
, lev
+ 1);
2443 struct dysymtab_command
*dc
= (struct dysymtab_command
*)lc
;
2444 iextdef
= dc
->iextdefsym
;
2445 nextdef
= dc
->nextdefsym
;
2449 lc
= (struct load_command
*) ((char*)lc
+ lc
->cmdsize
);
2452 if (tcc_add_dllref(s1
, soname
, lev
)->found
)
2455 if (!nsyms
|| !nextdef
)
2456 tcc_warning("%s doesn't export any symbols?", filename
);
2458 //dprintf("symbols (all):\n");
2459 dprintf("symbols (exported):\n");
2460 dprintf(" n: typ sec desc value name\n");
2461 //for (i = 0; i < nsyms; i++) {
2462 for (i
= iextdef
; i
< iextdef
+ nextdef
; i
++) {
2463 struct nlist_64
*sym
= symtab
+ i
;
2464 dprintf("%5d: %3d %3d 0x%04x 0x%016lx %s\n",
2465 i
, sym
->n_type
, sym
->n_sect
, sym
->n_desc
, (long)sym
->n_value
,
2466 strtab
+ sym
->n_strx
);
2467 set_elf_sym(s1
->dynsymtab_section
, 0, 0,
2468 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
),
2469 0, SHN_UNDEF
, strtab
+ sym
->n_strx
);