Skip to content

Commit cc98775

Browse files
[libc] Add link.h and elf.h
1 parent 75bc20f commit cc98775

24 files changed

+325
-3
lines changed

libc/config/linux/aarch64/entrypoints.txt

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(TARGET_LIBC_ENTRYPOINTS
1818
libc.src.ctype.toupper
1919

2020
# dlfcn.h entrypoints
21+
libc.src.dlfcn.dladdr
2122
libc.src.dlfcn.dlclose
2223
libc.src.dlfcn.dlerror
2324
libc.src.dlfcn.dlopen
@@ -32,6 +33,9 @@ set(TARGET_LIBC_ENTRYPOINTS
3233
libc.src.fcntl.open
3334
libc.src.fcntl.openat
3435

36+
# link.h entrypoints
37+
libc.src.link.dl_iterate_phdr
38+
3539
# sched.h entrypoints
3640
libc.src.sched.sched_get_priority_max
3741
libc.src.sched.sched_get_priority_min

libc/config/linux/aarch64/headers.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ set(TARGET_PUBLIC_HEADERS
22
libc.include.assert
33
libc.include.ctype
44
libc.include.dlfcn
5+
libc.include.elf
56
libc.include.errno
67
libc.include.features
78
libc.include.fenv
89
libc.include.float
910
libc.include.stdint
1011
libc.include.inttypes
1112
libc.include.limits
13+
libc.include.link
1214
libc.include.math
1315
libc.include.pthread
1416
libc.include.signal

libc/config/linux/api.td

+13
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,16 @@ def SearchAPI : PublicAPI<"search.h"> {
308308
def SysStatvfsAPI : PublicAPI<"sys/statvfs.h"> {
309309
let Types = ["fsblkcnt_t", "fsfilcnt_t", "struct statvfs"];
310310
}
311+
312+
def LinkAPI : PublicAPI<"link.h"> {
313+
let Types = [
314+
"struct dl_phdr_info",
315+
"__dl_iterate_phdr_callback_t"
316+
];
317+
}
318+
319+
def DlfcnAPI : PublicAPI<"dlfcn.h"> {
320+
let Types = [
321+
"Dl_info"
322+
];
323+
}

libc/config/linux/x86_64/entrypoints.txt

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(TARGET_LIBC_ENTRYPOINTS
1818
libc.src.ctype.toupper
1919

2020
# dlfcn.h entrypoints
21+
libc.src.dlfcn.dladdr
2122
libc.src.dlfcn.dlclose
2223
libc.src.dlfcn.dlerror
2324
libc.src.dlfcn.dlopen
@@ -32,6 +33,9 @@ set(TARGET_LIBC_ENTRYPOINTS
3233
libc.src.fcntl.open
3334
libc.src.fcntl.openat
3435

36+
# link.h entrypoints
37+
libc.src.link.dl_iterate_phdr
38+
3539
# sched.h entrypoints
3640
libc.src.sched.sched_get_priority_max
3741
libc.src.sched.sched_get_priority_min

libc/config/linux/x86_64/headers.txt

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(TARGET_PUBLIC_HEADERS
33
libc.include.ctype
44
libc.include.dirent
55
libc.include.dlfcn
6+
libc.include.elf
67
libc.include.errno
78
libc.include.fcntl
89
libc.include.features
@@ -11,6 +12,7 @@ set(TARGET_PUBLIC_HEADERS
1112
libc.include.stdint
1213
libc.include.inttypes
1314
libc.include.limits
15+
libc.include.link
1416
libc.include.math
1517
libc.include.pthread
1618
libc.include.sched

libc/include/CMakeLists.txt

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ add_gen_header(
5656
DEF_FILE dlfcn.h.def
5757
GEN_HDR dlfcn.h
5858
DEPENDS
59+
.llvm-libc-types.Dl_info
5960
.llvm-libc-macros.dlfcn_macros
6061
.llvm_libc_common_h
6162
)
@@ -367,6 +368,25 @@ add_gen_header(
367368
.llvm-libc-types.posix_spawn_file_actions_t
368369
)
369370

371+
add_gen_header(
372+
link
373+
DEF_FILE link.h.def
374+
GEN_HDR link.h
375+
DEPENDS
376+
.llvm_libc_common_h
377+
.llvm-libc-types.struct_dl_phdr_info
378+
.llvm-libc-types.__dl_iterate_phdr_callback_t
379+
.llvm-libc-macros.link_macros
380+
)
381+
382+
add_gen_header(
383+
elf
384+
DEF_FILE elf.h.def
385+
GEN_HDR elf.h
386+
DEPENDS
387+
.llvm-libc-macros.elf_macros
388+
)
389+
370390
# TODO: Not all platforms will have a include/sys directory. Add the sys
371391
# directory and the targets for sys/*.h files conditional to the OS requiring
372392
# them.

libc/include/elf.h.def

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- C standard library header elf.h -----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_ELF_H
10+
#define LLVM_LIBC_ELF_H
11+
12+
#include "llvm-libc-macros/elf-macros.h"
13+
14+
#endif // LLVM_LIBC_ELF_H

libc/include/link.h.def

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- C standard library header link.h ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_LINK_H
10+
#define LLVM_LIBC_LINK_H
11+
12+
#include "llvm-libc-macros/link-macros.h"
13+
14+
%%public_api()
15+
16+
#endif // LLVM_LIBC_LINK_H

libc/include/llvm-libc-macros/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,9 @@ add_macro_header(
283283
HDR
284284
dlfcn-macros.h
285285
)
286+
287+
add_macro_header(
288+
elf_macros
289+
HDR
290+
elf-macros.h
291+
)
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Definition of macros from elf.h -----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_MACROS_ELF_MACROS_H
10+
#define LLVM_LIBC_MACROS_ELF_MACROS_H
11+
12+
#if __has_include(<linux/elf.h>)
13+
#include <linux/elf.h>
14+
#else
15+
#error "cannot use <sys/elf.h> without proper system headers."
16+
#endif
17+
18+
#endif // LLVM_LIBC_MACROS_ELF_MACROS_H

libc/include/llvm-libc-macros/link-macros.h

+24-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,30 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef LLVM_LIBC_MACROS_LINK_MACROS_H
10+
#define LLVM_LIBC_MACROS_LINK_MACROS_H
11+
12+
#include "elf-macros.h"
13+
914
#ifdef __LP64__
10-
#define ElfW(type) Elf64_ ## type
15+
#define ElfW(type) Elf64_##type
1116
#else
12-
#define ElfW(type) Elf32_ ## type
17+
#define ElfW(type) Elf32_##type
18+
#endif
19+
20+
struct link_map {
21+
ElfW(Addr) l_addr;
22+
char *l_name;
23+
ElfW(Dyn) * l_ld;
24+
struct link_map *l_next, *l_prev;
25+
};
26+
27+
struct r_debug {
28+
int r_version;
29+
struct link_map *r_map;
30+
ElfW(Addr) r_brk;
31+
enum { RT_CONSISTENT, RT_ADD, RT_DELETE } r_state;
32+
ElfW(Addr) r_ldbase;
33+
};
34+
1335
#endif

libc/include/llvm-libc-types/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ add_header(thrd_t HDR thrd_t.h DEPENDS .__thread_type)
8989
add_header(tss_t HDR tss_t.h)
9090
add_header(tss_dtor_t HDR tss_dtor_t.h)
9191
add_header(__atexithandler_t HDR __atexithandler_t.h)
92+
add_header(Dl_info HDR Dl_info.h)
93+
add_header(struct_dl_phdr_info HDR struct_dl_phdr_info.h)
94+
add_header(__dl_iterate_phdr_callback_t HDR __dl_iterate_phdr_callback_t.h)
9295
add_header(speed_t HDR speed_t.h)
9396
add_header(tcflag_t HDR tcflag_t.h)
9497
add_header(struct_termios HDR struct_termios.h DEPENDS .cc_t .speed_t .tcflag_t)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Definition of type Dl_info ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_DL_INFO_H
10+
#define LLVM_LIBC_TYPES_DL_INFO_H
11+
12+
typedef struct {
13+
const char *dli_fname;
14+
void *dli_fbase;
15+
const char *dli_sname;
16+
void *dli_saddr;
17+
} Dl_info;
18+
19+
#endif // LLVM_LIBC_TYPES_DL_INFO_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Definition of __dl_iterate_phdr_callback_t type -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES___DL_ITERATE_PHDR_CALLBACK_T_H
10+
#define LLVM_LIBC_TYPES___DL_ITERATE_PHDR_CALLBACK_T_H
11+
12+
#include "llvm-libc-types/size_t.h"
13+
14+
typedef int (*__dl_iterate_phdr_callback_t)(struct dl_phdr_info *info,
15+
size_t size, void *data);
16+
17+
#endif // LLVM_LIBC_TYPES___DL_ITERATE_PHDR_CALLBACK_T_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Definition of type struct dl_phdr_info ----------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_STRUCT_DL_PHDR_INFO_H
10+
#define LLVM_LIBC_TYPES_STRUCT_DL_PHDR_INFO_H
11+
12+
#include "llvm-libc-macros/link-macros.h"
13+
#include "llvm-libc-types/size_t.h"
14+
15+
struct dl_phdr_info {
16+
ElfW(Addr) dlpi_addr;
17+
const char *dlpi_name;
18+
const ElfW(Phdr) * dlpi_phdr;
19+
ElfW(Half) dlpi_phnum;
20+
unsigned long long int dlpi_adds;
21+
unsigned long long int dlpi_subs;
22+
size_t dlpi_tls_modid;
23+
void *dlpi_tls_data;
24+
};
25+
26+
#endif // LLVM_LIBC_TYPES_STRUCT_DL_PHDR_INFO_H

libc/spec/linux.td

+30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ def StructEpollEventPtr : PtrType<StructEpollEvent>;
33

44
def StructEpollData : NamedType<"struct epoll_data">;
55

6+
def StructDlPhdrInfo : NamedType<"struct dl_phdr_info">;
7+
def DlIteratePhdrCallback : NamedType<"__dl_iterate_phdr_callback_t">;
8+
69
def Linux : StandardSpec<"Linux"> {
710
HeaderSpec Errno = HeaderSpec<
811
"errno.h",
@@ -264,8 +267,35 @@ def Linux : StandardSpec<"Linux"> {
264267
]
265268
>;
266269

270+
HeaderSpec Link = HeaderSpec<
271+
"link.h",
272+
[], // Macros
273+
[StructDlPhdrInfo, DlIteratePhdrCallback], // Types
274+
[], // Enumerations
275+
[
276+
FunctionSpec<
277+
"dl_iterate_phdr",
278+
RetValSpec<IntType>,
279+
[
280+
ArgSpec<DlIteratePhdrCallback>,
281+
ArgSpec<VoidPtr>
282+
]
283+
>,
284+
] // Functions
285+
>;
286+
287+
HeaderSpec Elf = HeaderSpec<
288+
"elf.h",
289+
[], // Macros
290+
[], // Types
291+
[], // Enumerations
292+
[] // Functions
293+
>;
294+
267295
let Headers = [
296+
Elf,
268297
Errno,
298+
Link,
269299
SysEpoll,
270300
SysMMan,
271301
SysPrctl,

libc/spec/posix.td

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def PThreadOnceT : NamedType<"pthread_once_t">;
2121
def PThreadOnceTPtr : PtrType<PThreadOnceT>;
2222
def PThreadOnceCallback : NamedType<"__pthread_once_func_t">;
2323

24+
def DlInfo : NamedType<"Dl_info">;
25+
def DlInfoPtr : PtrType<DlInfo>;
26+
2427
def InoT : NamedType<"ino_t">;
2528
def UidT : NamedType<"uid_t">;
2629
def GidT : NamedType<"gid_t">;
@@ -230,9 +233,14 @@ def POSIX : StandardSpec<"POSIX"> {
230233
Macro<"RTLD_GLOBAL">,
231234
Macro<"RTLD_LOCAL">,
232235
],
233-
[], // Types
236+
[DlInfo], // Types
234237
[], // Enumerations
235238
[
239+
FunctionSpec<
240+
"dladdr",
241+
RetValSpec<IntType>,
242+
[ArgSpec<ConstVoidPtr>, ArgSpec<DlInfoPtr>]
243+
>,
236244
FunctionSpec<
237245
"dlclose",
238246
RetValSpec<IntType>,

libc/src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_subdirectory(dlfcn)
55
add_subdirectory(errno)
66
add_subdirectory(fenv)
77
add_subdirectory(inttypes)
8+
add_subdirectory(link)
89
add_subdirectory(math)
910
add_subdirectory(stdbit)
1011
add_subdirectory(stdfix)

libc/src/dlfcn/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
add_entrypoint_object(
2+
dladdr
3+
SRCS
4+
dladdr.cpp
5+
HDRS
6+
dladdr.h
7+
DEPENDS
8+
libc.include.dlfcn
9+
libc.src.errno.errno
10+
)
11+
112
add_entrypoint_object(
213
dlclose
314
SRCS

0 commit comments

Comments
 (0)