Skip to content

Commit 5a9a08d

Browse files
committed
Fix regression in elf2pe program
1 parent bd6d9ff commit 5a9a08d

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

libc/mem/tinymalloc.inc

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "libc/assert.h"
1717
#include "libc/dce.h"
1818
#include "libc/errno.h"
19+
#include "libc/intrin/kprintf.h"
1920
#include "libc/mem/mem.h"
2021
#include "libc/stdalign.internal.h"
2122
#include "libc/stdckdint.h"
@@ -26,15 +27,29 @@
2627
#endif
2728

2829
#ifndef TINYMALLOC_MAX_ALIGN
29-
#define TINYMALLOC_MAX_ALIGN 4096
30+
#define TINYMALLOC_MAX_ALIGN sizeof(max_align_t)
3031
#endif
3132

32-
alignas(TINYMALLOC_MAX_ALIGN) static struct {
33-
char memory[TINYMALLOC_MAX_BYTES];
34-
size_t used, last, free;
33+
static struct {
34+
alignas(max_align_t) char bits[TINYMALLOC_MAX_BYTES];
35+
char *memory;
36+
int once;
37+
size_t size, used, last, free;
3538
} heap;
3639

37-
static inline bool isheap(char *mem) {
40+
static void tinymalloc_init(void) {
41+
int align;
42+
if (heap.once)
43+
return;
44+
align = TINYMALLOC_MAX_ALIGN;
45+
heap.memory = (char *)(((uintptr_t)heap.bits + align - 1) & -align);
46+
heap.size = sizeof(heap.bits) - (heap.memory - heap.bits);
47+
kprintf("heap.memory = %p\n", heap.memory);
48+
kprintf("heap.size = %p\n", heap.size);
49+
heap.once = 1;
50+
}
51+
52+
static inline int isheap(char *mem) {
3853
return heap.memory <= mem && mem < heap.memory + heap.used;
3954
}
4055

@@ -59,6 +74,7 @@ size_t malloc_usable_size(void *ptr) {
5974
void *memalign(size_t align, size_t need) {
6075
char *res;
6176
size_t next, next2, base, toto, *link, *link2;
77+
tinymalloc_init();
6278

6379
// normalize arguments
6480
while (align & (align - 1))
@@ -95,7 +111,7 @@ void *memalign(size_t align, size_t need) {
95111
base &= -align;
96112
if (ckd_add(&toto, base, need))
97113
goto OutOfMemory;
98-
if (toto > TINYMALLOC_MAX_BYTES)
114+
if (toto > heap.size)
99115
goto OutOfMemory;
100116
res = heap.memory + base;
101117
((size_t *)res)[-1] = need;
@@ -148,7 +164,7 @@ void *realloc(void *ptr, size_t need) {
148164
need &= -sizeof(size_t);
149165
if (ckd_add(&toto, base, need))
150166
goto OutOfMemory;
151-
if (toto > TINYMALLOC_MAX_BYTES)
167+
if (toto > heap.size)
152168
goto OutOfMemory;
153169
((size_t *)mem)[-1] = need;
154170
heap.used = toto;

tool/build/elf2pe.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "libc/fmt/itoa.h"
2828
#include "libc/intrin/describeflags.internal.h"
2929
#include "libc/intrin/dll.h"
30+
#include "libc/intrin/kprintf.h"
3031
#include "libc/limits.h"
3132
#include "libc/macros.internal.h"
3233
#include "libc/mem/mem.h"
@@ -158,6 +159,7 @@ static const char *stubpath;
158159
static long FLAG_SizeOfStackCommit = 64 * 1024;
159160
static long FLAG_SizeOfStackReserve = 8 * 1024 * 1024;
160161

162+
#define TINYMALLOC_MAX_ALIGN MAX_ALIGN
161163
#include "libc/mem/tinymalloc.inc"
162164

163165
static wontreturn void Die(const char *thing, const char *reason) {
@@ -186,6 +188,13 @@ static void *Calloc(size_t n) {
186188
return p;
187189
}
188190

191+
static void *Memalign(size_t a, size_t n) {
192+
void *p;
193+
if (!(p = memalign(a, n)))
194+
DieOom();
195+
return p;
196+
}
197+
189198
static void *Realloc(void *p, size_t n) {
190199
if (!(p = realloc(p, n)))
191200
DieOom();
@@ -1106,7 +1115,7 @@ int main(int argc, char *argv[]) {
11061115
GetOpts(argc, argv);
11071116
// translate executable
11081117
struct Elf *elf = OpenElf(argv[optind]);
1109-
char *buf = memalign(MAX_ALIGN, 134217728);
1118+
char *buf = Memalign(MAX_ALIGN, 134217728);
11101119
struct ImagePointer ip = GeneratePe(elf, buf, 0x00400000);
11111120
if (creat(outpath, 0755) == -1)
11121121
DieSys(elf->path);

0 commit comments

Comments
 (0)