Skip to content

Commit 81c36bf

Browse files
committed
Merge branch 'main' into new_ldr
2 parents eb782cc + 4aa3bb7 commit 81c36bf

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD 11)
55
set(CMAKE_C_STANDARD_REQUIRED ON)
66
project(dl)
77

8-
CPMAddPackage("gh:kynex7510/CTRL#b37a459")
8+
CPMAddPackage("gh:kynex7510/CTRL#3b5ed7e")
99

1010
set(DL_SOURCES
1111
Source/API.c

Include/dlfcn.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct {
3232

3333
#if defined(__cplusplus)
3434
extern "C" {
35-
#endif
35+
#endif // __cplusplus
3636

3737
void* dlopen(const char* path, int flags);
3838
int dlclose(void* handle);
@@ -52,6 +52,6 @@ void ctrdlFreeInfo(CTRDLInfo* info);
5252

5353
#if defined(__cplusplus)
5454
}
55-
#endif
55+
#endif // cplusplus
5656

5757
#endif /* _CTRDL_DLFCN_H */

Source/Loader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifdef CTRDL_RESERVED_CODE_PAGES
1313
u32 __ctrl_code_allocator_pages = CTRDL_RESERVED_CODE_PAGES;
1414
#else
15-
u32 __ctrl_code_allocator_pages = 128; // Default to 512kb.
15+
u32 __ctrl_code_allocator_pages = 256; // Default to 1MB.
1616
#endif // CTRDL_RESERVED_CODE_PAGES
1717

1818
typedef struct {

Source/Relocs.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "Relocs.h"
22
#include "Symbol.h"
33

4+
#include <string.h> // strcmp
5+
46
typedef struct {
57
CTRDLHandle* handle;
68
CTRDLElf* elf;
@@ -23,6 +25,7 @@ static u32 ctrdl_resolveSymbol(const RelContext* ctx, Elf32_Word index, bool* is
2325
return 0;
2426
}
2527

28+
u32 symBase = 0;
2629
const Elf32_Sym* symEntry = &ctx->elf->symEntries[index];
2730
const char* name = &ctx->elf->stringTable[symEntry->st_name];
2831
*isWeak = ELF32_ST_BIND(symEntry->st_info) == STB_WEAK;
@@ -47,20 +50,40 @@ static u32 ctrdl_resolveSymbol(const RelContext* ctx, Elf32_Word index, bool* is
4750
CTRDLHandle* h = ctrdl_unsafeGetHandleByIndex(i);
4851
if (h->flags & RTLD_GLOBAL) {
4952
sym = ctrdl_symNameLookupSingle(h, name);
50-
if (sym)
53+
if (sym) {
54+
symBase = h->base;
5155
break;
56+
}
5257
}
5358
}
5459

5560
ctrdl_releaseHandleMtx();
5661

62+
if (!sym) {
63+
// Look into ourselves.
64+
if (ctx->elf->numSymChains) {
65+
const Elf32_Word hash = ctrdl_getELFSymNameHash(name);
66+
size_t chainIndex = ctx->elf->symBuckets[hash % ctx->elf->numSymBuckets];
67+
68+
while (chainIndex != STN_UNDEF) {
69+
const Elf32_Sym* candidate = &ctx->elf->symEntries[chainIndex];
70+
if (!strcmp(&ctx->elf->stringTable[candidate->st_name], name)) {
71+
sym = candidate;
72+
symBase = ctx->handle->base;
73+
break;
74+
}
75+
76+
chainIndex = ctx->elf->symChains[chainIndex];
77+
}
78+
}
79+
}
80+
5781
if (!sym) {
5882
// Look into dependencies.
59-
// TODO: sym info for current module is not setup; do we need to look into ourselves?
60-
sym = ctrdl_symNameLookupLoadOrder(ctx->handle, name);
83+
sym = ctrdl_symNameLookupLoadOrder(ctx->handle, name, &symBase);
6184
}
6285

63-
return sym ? (ctx->handle->base + sym->st_value) : 0;
86+
return sym ? (symBase + sym->st_value) : 0;
6487
}
6588

6689
static bool ctrdl_handleSingleReloc(RelContext* ctx, RelEntry* entry) {

Source/Symbol.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const Elf32_Sym* ctrdl_symNameLookupSingle(CTRDLHandle* handle, const char* name
6464
return found;
6565
}
6666

67-
const Elf32_Sym* ctrdl_symNameLookupLoadOrder(CTRDLHandle* handle, const char* name) {
67+
const Elf32_Sym* ctrdl_symNameLookupLoadOrder(CTRDLHandle* handle, const char* name, u32* modBase) {
6868
const Elf32_Sym* found = NULL;
6969

7070
if (handle) {
@@ -73,10 +73,12 @@ const Elf32_Sym* ctrdl_symNameLookupLoadOrder(CTRDLHandle* handle, const char* n
7373
found = ctrdl_symNameLookupSingle(handle, name);
7474
if (!found) {
7575
for (size_t i = 0; i < CTRDL_MAX_DEPS; ++i) {
76-
found = ctrdl_symNameLookupLoadOrder(handle->deps[i], name);
76+
found = ctrdl_symNameLookupLoadOrder(handle->deps[i], name, modBase);
7777
if (found)
7878
break;
7979
}
80+
} else if (modBase) {
81+
*modBase = handle->base;
8082
}
8183

8284
ctrdl_unlockHandle(handle);

Source/Symbol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "Handle.h"
55

66
const Elf32_Sym* ctrdl_symNameLookupSingle(CTRDLHandle* handle, const char* name);
7-
const Elf32_Sym* ctrdl_symNameLookupLoadOrder(CTRDLHandle* handle, const char* name);
7+
const Elf32_Sym* ctrdl_symNameLookupLoadOrder(CTRDLHandle* handle, const char* name, u32* modBase);
88
const Elf32_Sym* ctrdl_symNameLookupDepOrder(CTRDLHandle* handle, const char* name);
99
const Elf32_Sym* ctrdl_symValueLookupSingle(CTRDLHandle* handle, Elf32_Word value);
1010

0 commit comments

Comments
 (0)