Fix regression in elf2pe program

This commit is contained in:
Justine Tunney 2024-07-04 04:02:20 -07:00
parent bd6d9ff99a
commit 5a9a08d1cf
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
2 changed files with 33 additions and 8 deletions

View file

@ -16,6 +16,7 @@
#include "libc/assert.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/mem/mem.h"
#include "libc/stdalign.internal.h"
#include "libc/stdckdint.h"
@ -26,15 +27,29 @@
#endif
#ifndef TINYMALLOC_MAX_ALIGN
#define TINYMALLOC_MAX_ALIGN 4096
#define TINYMALLOC_MAX_ALIGN sizeof(max_align_t)
#endif
alignas(TINYMALLOC_MAX_ALIGN) static struct {
char memory[TINYMALLOC_MAX_BYTES];
size_t used, last, free;
static struct {
alignas(max_align_t) char bits[TINYMALLOC_MAX_BYTES];
char *memory;
int once;
size_t size, used, last, free;
} heap;
static inline bool isheap(char *mem) {
static void tinymalloc_init(void) {
int align;
if (heap.once)
return;
align = TINYMALLOC_MAX_ALIGN;
heap.memory = (char *)(((uintptr_t)heap.bits + align - 1) & -align);
heap.size = sizeof(heap.bits) - (heap.memory - heap.bits);
kprintf("heap.memory = %p\n", heap.memory);
kprintf("heap.size = %p\n", heap.size);
heap.once = 1;
}
static inline int isheap(char *mem) {
return heap.memory <= mem && mem < heap.memory + heap.used;
}
@ -59,6 +74,7 @@ size_t malloc_usable_size(void *ptr) {
void *memalign(size_t align, size_t need) {
char *res;
size_t next, next2, base, toto, *link, *link2;
tinymalloc_init();
// normalize arguments
while (align & (align - 1))
@ -95,7 +111,7 @@ void *memalign(size_t align, size_t need) {
base &= -align;
if (ckd_add(&toto, base, need))
goto OutOfMemory;
if (toto > TINYMALLOC_MAX_BYTES)
if (toto > heap.size)
goto OutOfMemory;
res = heap.memory + base;
((size_t *)res)[-1] = need;
@ -148,7 +164,7 @@ void *realloc(void *ptr, size_t need) {
need &= -sizeof(size_t);
if (ckd_add(&toto, base, need))
goto OutOfMemory;
if (toto > TINYMALLOC_MAX_BYTES)
if (toto > heap.size)
goto OutOfMemory;
((size_t *)mem)[-1] = need;
heap.used = toto;

View file

@ -27,6 +27,7 @@
#include "libc/fmt/itoa.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/kprintf.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
@ -158,6 +159,7 @@ static const char *stubpath;
static long FLAG_SizeOfStackCommit = 64 * 1024;
static long FLAG_SizeOfStackReserve = 8 * 1024 * 1024;
#define TINYMALLOC_MAX_ALIGN MAX_ALIGN
#include "libc/mem/tinymalloc.inc"
static wontreturn void Die(const char *thing, const char *reason) {
@ -186,6 +188,13 @@ static void *Calloc(size_t n) {
return p;
}
static void *Memalign(size_t a, size_t n) {
void *p;
if (!(p = memalign(a, n)))
DieOom();
return p;
}
static void *Realloc(void *p, size_t n) {
if (!(p = realloc(p, n)))
DieOom();
@ -1106,7 +1115,7 @@ int main(int argc, char *argv[]) {
GetOpts(argc, argv);
// translate executable
struct Elf *elf = OpenElf(argv[optind]);
char *buf = memalign(MAX_ALIGN, 134217728);
char *buf = Memalign(MAX_ALIGN, 134217728);
struct ImagePointer ip = GeneratePe(elf, buf, 0x00400000);
if (creat(outpath, 0755) == -1)
DieSys(elf->path);