From 8874a37abc82da38e0e8923c3b611afb95b4667e Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Fri, 8 Dec 2023 12:52:24 -0800 Subject: [PATCH] Add for absl --- ape/ape.S | 2 ++ ape/sections.internal.h | 1 + libc/calls/dl_iterate_phdr.c | 33 +++++++++++++++++++++++++++++ libc/elf/link.h | 40 ++++++++++++++++++++++++++++++++++++ libc/isystem/link.h | 23 +++++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 libc/calls/dl_iterate_phdr.c create mode 100644 libc/elf/link.h create mode 100644 libc/isystem/link.h diff --git a/ape/ape.S b/ape/ape.S index 9a680105f..70b3d709c 100644 --- a/ape/ape.S +++ b/ape/ape.S @@ -752,6 +752,8 @@ ape_loader_end: #if SupportsSystemv() || SupportsMetal() .section .elf.phdrs,"a",@progbits + .globl ape_phdrs +ape_phdrs: .long PT_LOAD .long PF_R|PF_X .stub ape_cod_offset,quad diff --git a/ape/sections.internal.h b/ape/sections.internal.h index 0fbf72e70..119c4990e 100644 --- a/ape/sections.internal.h +++ b/ape/sections.internal.h @@ -22,6 +22,7 @@ extern uint8_t __data_start[] __attribute__((__weak__)); extern uint8_t __data_end[] __attribute__((__weak__)); extern uint8_t __bss_start[] __attribute__((__weak__)); extern uint8_t __bss_end[] __attribute__((__weak__)); +extern unsigned char ape_phdrs[] __attribute__((__weak__)); COSMOPOLITAN_C_END_ #endif /* COSMOPOLITAN_APE_SECTIONS_INTERNAL_H_ */ diff --git a/libc/calls/dl_iterate_phdr.c b/libc/calls/dl_iterate_phdr.c new file mode 100644 index 000000000..a62bb98ff --- /dev/null +++ b/libc/calls/dl_iterate_phdr.c @@ -0,0 +1,33 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" +#include "libc/elf/link.h" +#include "libc/runtime/runtime.h" + +int dl_iterate_phdr(int callback(struct dl_phdr_info *info, size_t size, + void *data), + void *data) { + struct dl_phdr_info info = { + .dlpi_addr = (uintptr_t)__executable_start, + .dlpi_name = GetProgramExecutableName(), + .dlpi_phdr = (const Elf64_Phdr *)ape_phdrs, + .dlpi_phnum = 3, + }; + return callback(&info, sizeof(info), data); +} diff --git a/libc/elf/link.h b/libc/elf/link.h new file mode 100644 index 000000000..d951627ae --- /dev/null +++ b/libc/elf/link.h @@ -0,0 +1,40 @@ +#ifndef COSMOPOLITAN_ELF_LINK_H_ +#define COSMOPOLITAN_ELF_LINK_H_ +#include "libc/elf/scalar.h" +#include "libc/elf/struct/dyn.h" +#include "libc/elf/struct/phdr.h" +COSMOPOLITAN_C_START_ + +#define ElfW(type) Elf64_##type + +struct dl_phdr_info { + Elf64_Addr dlpi_addr; + const char *dlpi_name; + const Elf64_Phdr *dlpi_phdr; + Elf64_Half dlpi_phnum; + unsigned long long int dlpi_adds; + unsigned long long int dlpi_subs; + size_t dlpi_tls_modid; + void *dlpi_tls_data; +}; + +struct link_map { + Elf64_Addr l_addr; + char *l_name; + Elf64_Dyn *l_ld; + struct link_map *l_next; + struct link_map *l_prev; +}; + +struct r_debug { + int r_version; + struct link_map *r_map; + Elf64_Addr r_brk; + enum { RT_CONSISTENT, RT_ADD, RT_DELETE } r_state; + Elf64_Addr r_ldbase; +}; + +int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *), void *); + +COSMOPOLITAN_C_END_ +#endif /* COSMOPOLITAN_ELF_LINK_H_ */ diff --git a/libc/isystem/link.h b/libc/isystem/link.h new file mode 100644 index 000000000..b3e06878d --- /dev/null +++ b/libc/isystem/link.h @@ -0,0 +1,23 @@ +#ifndef _LINK_H +#define _LINK_H +#include "libc/elf/def.h" +#include "libc/elf/link.h" +#include "libc/elf/scalar.h" +#include "libc/elf/struct/auxv.h" +#include "libc/elf/struct/chdr.h" +#include "libc/elf/struct/dyn.h" +#include "libc/elf/struct/ehdr.h" +#include "libc/elf/struct/lib.h" +#include "libc/elf/struct/move.h" +#include "libc/elf/struct/nhdr.h" +#include "libc/elf/struct/phdr.h" +#include "libc/elf/struct/rel.h" +#include "libc/elf/struct/rela.h" +#include "libc/elf/struct/shdr.h" +#include "libc/elf/struct/sym.h" +#include "libc/elf/struct/syminfo.h" +#include "libc/elf/struct/verdaux.h" +#include "libc/elf/struct/verdef.h" +#include "libc/elf/struct/vernaux.h" +#include "libc/elf/struct/verneed.h" +#endif /* _LINK_H */