| @@ -1413,7 +1413,9 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) · 1412 unmodified lines |
| 1413 | 1413 | #ifdef CONFIG_TCC_BCHECK |
| 1414 | 1414 | if (s1->do_bounds_check && s1->output_type != TCC_OUTPUT_DLL) { |
| 1415 | 1415 | tcc_add_library_err(s1, "pthread"); |
| 1416 | +#if !defined(__OpenBSD__) |
| 1416 | 1417 | tcc_add_library_err(s1, "dl"); |
| 1418 | +#endif |
| 1417 | 1419 | tcc_add_support(s1, "bcheck.o"); |
| 1418 | 1420 | } |
| 1419 | 1421 | #endif |
| @@ -1992,7 +1994,7 @@ static void put_dt(Section *dynamic, int dt, addr_t val) · 572 unmodified lines |
| 1992 | 1994 | } |
| 1993 | 1995 | |
| 1994 | 1996 | static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp, |
| 1995 | | − Section *dynamic) |
| 1997 | + Section *dynamic, Section *note) |
| 1996 | 1998 | { |
| 1997 | 1999 | ElfW(Phdr) *ph; |
| 1998 | 2000 | |
| @@ -2019,6 +2021,19 @@ static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp, · 20 unmodified lines |
| 2019 | 2021 | ph->p_align = interp->sh_addralign; |
| 2020 | 2022 | } |
| 2021 | 2023 | |
| 2024 | + if (note) { |
| 2025 | + ph = &phdr[phnum - 2]; |
| 2026 | + |
| 2027 | + ph->p_type = PT_NOTE; |
| 2028 | + ph->p_offset = note->sh_offset; |
| 2029 | + ph->p_vaddr = note->sh_addr; |
| 2030 | + ph->p_paddr = ph->p_vaddr; |
| 2031 | + ph->p_filesz = note->sh_size; |
| 2032 | + ph->p_memsz = note->sh_size; |
| 2033 | + ph->p_flags = PF_R; |
| 2034 | + ph->p_align = note->sh_addralign; |
| 2035 | + } |
| 2036 | + |
| 2022 | 2037 | /* if dynamic section, then add corresponding program header */ |
| 2023 | 2038 | if (dynamic) { |
| 2024 | 2039 | ph = &phdr[phnum - 1]; |
| @@ -2051,6 +2066,13 @@ static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf) · 26 unmodified lines |
| 2051 | 2066 | put_dt(dynamic, DT_RELA, dyninf->rel_addr); |
| 2052 | 2067 | put_dt(dynamic, DT_RELASZ, dyninf->rel_size); |
| 2053 | 2068 | put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel)); |
| 2069 | +#ifdef __OpenBSD__ |
| 2070 | + put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr); |
| 2071 | + put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size); |
| 2072 | + put_dt(dynamic, DT_JMPREL, dyninf->rel_addr); |
| 2073 | + put_dt(dynamic, DT_PLTREL, DT_RELA); |
| 2074 | + put_dt(dynamic, DT_BIND_NOW, 1); /* Dirty hack */ |
| 2075 | +#endif |
| 2054 | 2076 | #else |
| 2055 | 2077 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 2056 | 2078 | put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr); |
| @@ -2372,6 +2394,25 @@ static void create_arm_attribute_section(TCCState *s1) · 315 unmodified lines |
| 2372 | 2394 | } |
| 2373 | 2395 | #endif |
| 2374 | 2396 | |
| 2397 | +#ifdef __OpenBSD__ |
| 2398 | +static Section *create_openbsd_note_section(TCCState *s1) |
| 2399 | +{ |
| 2400 | + Section *s = find_section (s1, ".note.openbsd.ident"); |
| 2401 | + |
| 2402 | + if (s->data_offset == 0) { |
| 2403 | + unsigned char *ptr = section_ptr_add(s, sizeof(ElfW(Nhdr)) + 8 + 4); |
| 2404 | + ElfW(Nhdr) *note = (ElfW(Nhdr) *) ptr; |
| 2405 | + |
| 2406 | + s->sh_type = SHT_NOTE; |
| 2407 | + note->n_namesz = 8; |
| 2408 | + note->n_descsz = 4; |
| 2409 | + note->n_type = ELF_NOTE_OS_GNU; |
| 2410 | + strcpy (ptr + sizeof(ElfW(Nhdr)), "OpenBSD"); |
| 2411 | + } |
| 2412 | + return s; |
| 2413 | +} |
| 2414 | +#endif |
| 2415 | + |
| 2375 | 2416 | /* Output an elf, coff or binary file */ |
| 2376 | 2417 | /* XXX: suppress unneeded sections */ |
| 2377 | 2418 | static int elf_output_file(TCCState *s1, const char *filename) |
| @@ -2379,13 +2420,18 @@ static int elf_output_file(TCCState *s1, const char *filename) · 1 unmodified line |
| 2379 | 2420 | int ret, phnum, shnum, file_type, file_offset, *sec_order; |
| 2380 | 2421 | struct dyn_inf dyninf = {0}; |
| 2381 | 2422 | ElfW(Phdr) *phdr; |
| 2382 | | − Section *strsec, *interp, *dynamic, *dynstr; |
| 2423 | + Section *strsec, *interp, *dynamic, *dynstr, *note = NULL; |
| 2424 | + |
| 2425 | + file_type = s1->output_type; |
| 2383 | 2426 | |
| 2384 | 2427 | #ifdef TCC_TARGET_ARM |
| 2385 | 2428 | create_arm_attribute_section (s1); |
| 2386 | 2429 | #endif |
| 2430 | +#ifdef __OpenBSD__ |
| 2431 | + if (file_type != TCC_OUTPUT_OBJ) |
| 2432 | + note = create_openbsd_note_section (s1); |
| 2433 | +#endif |
| 2387 | 2434 | |
| 2388 | | − file_type = s1->output_type; |
| 2389 | 2435 | s1->nb_errors = 0; |
| 2390 | 2436 | ret = -1; |
| 2391 | 2437 | phdr = NULL; |
| @@ -2497,6 +2543,8 @@ static int elf_output_file(TCCState *s1, const char *filename) · 105 unmodified lines |
| 2497 | 2543 | phnum = i < s1->nb_sections ? 6 : 5; |
| 2498 | 2544 | } |
| 2499 | 2545 | |
| 2546 | + phnum += note != NULL; |
| 2547 | + |
| 2500 | 2548 | /* allocate program segment headers */ |
| 2501 | 2549 | phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr))); |
| 2502 | 2550 | |
| @@ -2515,7 +2563,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 12 unmodified lines |
| 2515 | 2563 | /* Fill remaining program header and finalize relocation related to dynamic |
| 2516 | 2564 | linking. */ |
| 2517 | 2565 | if (file_type != TCC_OUTPUT_OBJ) { |
| 2518 | | − fill_unloadable_phdr(phdr, phnum, interp, dynamic); |
| 2566 | + fill_unloadable_phdr(phdr, phnum, interp, dynamic, note); |
| 2519 | 2567 | if (dynamic) { |
| 2520 | 2568 | ElfW(Sym) *sym; |
| 2521 | 2569 | dynamic->data_offset = dyninf.data_offset; |
| @@ -2703,6 +2751,10 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 181 unmodified lines |
| 2703 | 2751 | if (sh->sh_type != SHT_PROGBITS && |
| 2704 | 2752 | #ifdef TCC_ARM_EABI |
| 2705 | 2753 | sh->sh_type != SHT_ARM_EXIDX && |
| 2754 | +#endif |
| 2755 | +#ifdef __OpenBSD__ |
| 2756 | + sh->sh_type != SHT_X86_64_UNWIND && |
| 2757 | + sh->sh_type != SHT_NOTE && |
| 2706 | 2758 | #endif |
| 2707 | 2759 | sh->sh_type != SHT_NOBITS && |
| 2708 | 2760 | sh->sh_type != SHT_PREINIT_ARRAY && |