Get garbage collector working on aarch64

Garbage collection will now happen on arm64 when a function returns,
rather than kicking the can down the road to when the process exits.
This change also does some code cleanup and incorporates suggestions
This commit is contained in:
Justine Tunney 2023-06-07 03:34:45 -07:00
parent 9793d3524f
commit 01fd655097
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
16 changed files with 144 additions and 323 deletions

View file

@ -18,10 +18,7 @@
*/
#include "ape/sections.internal.h"
#include "libc/calls/calls.h"
#include "libc/elf/def.h"
#include "libc/elf/elf.h"
#include "libc/elf/struct/ehdr.h"
#include "libc/elf/struct/sym.h"
#include "libc/elf/tinyelf.internal.h"
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/macros.internal.h"
@ -32,23 +29,6 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
static bool GetElfSymbolValue(const Elf64_Ehdr *ehdr, size_t esize,
const char *name, uint64_t *res) {
Elf64_Xword i, n;
const char *stab;
const Elf64_Sym *st;
if ((stab = GetElfStringTable(ehdr, esize)) &&
(st = GetElfSymbolTable(ehdr, esize, &n))) {
for (i = 0; i < n; ++i) {
if (!strcmp(stab + st[i].st_name, name)) {
*res = st[i].st_value;
return true;
}
}
}
return false;
}
static bool IsMyDebugBinaryImpl(const char *path) {
int fd;
void *map;
@ -61,8 +41,8 @@ static bool IsMyDebugBinaryImpl(const char *path) {
// which is currently running in memory.
if ((size = lseek(fd, 0, SEEK_END)) != -1 &&
(map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED) {
if (IsElf64Binary(map, size) &&
GetElfSymbolValue(map, size, "_etext", &value)) {
if (READ32LE(map) == READ32LE("\177ELF") &&
GetElfSymbolValue(map, "_etext", &value)) {
res = !_etext || value == (uintptr_t)_etext;
}
munmap(map, size);

View file

@ -20,11 +20,7 @@
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/elf/def.h"
#include "libc/elf/scalar.h"
#include "libc/elf/struct/phdr.h"
#include "libc/elf/struct/shdr.h"
#include "libc/elf/struct/sym.h"
#include "libc/elf/tinyelf.internal.h"
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/strace.internal.h"
@ -41,66 +37,6 @@
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/errfuns.h"
#define GetStr(tab, rva) ((char *)(tab) + (rva))
#define GetSection(e, s) ((void *)((intptr_t)(e) + (size_t)(s)->sh_offset))
#define GetShstrtab(e) GetSection(e, GetShdr(e, (e)->e_shstrndx))
#define GetSectionName(e, s) GetStr(GetShstrtab(e), (s)->sh_name)
#define GetPhdr(e, i) \
((Elf64_Phdr *)((intptr_t)(e) + (e)->e_phoff + \
(size_t)(e)->e_phentsize * (i)))
#define GetShdr(e, i) \
((Elf64_Shdr *)((intptr_t)(e) + (e)->e_shoff + \
(size_t)(e)->e_shentsize * (i)))
static char *GetStrtab(Elf64_Ehdr *e, size_t *n) {
char *name;
Elf64_Half i;
Elf64_Shdr *shdr;
for (i = 0; i < e->e_shnum; ++i) {
shdr = GetShdr(e, i);
if (shdr->sh_type == SHT_STRTAB) {
name = GetSectionName(e, GetShdr(e, i));
if (name && !__strcmp(name, ".strtab")) {
if (n) *n = shdr->sh_size;
return GetSection(e, shdr);
}
}
}
return 0;
}
static Elf64_Sym *GetSymtab(Elf64_Ehdr *e, Elf64_Xword *n) {
Elf64_Half i;
Elf64_Shdr *shdr;
for (i = e->e_shnum; i > 0; --i) {
shdr = GetShdr(e, i - 1);
if (shdr->sh_type == SHT_SYMTAB) {
if (shdr->sh_entsize != sizeof(Elf64_Sym)) continue;
if (n) *n = shdr->sh_size / shdr->sh_entsize;
return GetSection(e, shdr);
}
}
return 0;
}
static void GetImageRange(Elf64_Ehdr *elf, intptr_t *x, intptr_t *y) {
unsigned i;
Elf64_Phdr *phdr;
intptr_t start, end, pstart, pend;
start = INTPTR_MAX;
end = 0;
for (i = 0; i < elf->e_phnum; ++i) {
phdr = GetPhdr(elf, i);
if (phdr->p_type != PT_LOAD) continue;
pstart = phdr->p_vaddr;
pend = phdr->p_vaddr + phdr->p_memsz;
if (pstart < start) start = pstart;
if (pend > end) end = pend;
}
if (x) *x = start;
if (y) *y = end;
}
static struct SymbolTable *OpenSymbolTableImpl(const char *filename) {
int fd;
void *map;