mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-12 01:08:00 +00:00
Fix regression in elf2pe program
This commit is contained in:
parent
bd6d9ff99a
commit
5a9a08d1cf
2 changed files with 33 additions and 8 deletions
|
@ -16,6 +16,7 @@
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
|
#include "libc/intrin/kprintf.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/stdalign.internal.h"
|
#include "libc/stdalign.internal.h"
|
||||||
#include "libc/stdckdint.h"
|
#include "libc/stdckdint.h"
|
||||||
|
@ -26,15 +27,29 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef TINYMALLOC_MAX_ALIGN
|
#ifndef TINYMALLOC_MAX_ALIGN
|
||||||
#define TINYMALLOC_MAX_ALIGN 4096
|
#define TINYMALLOC_MAX_ALIGN sizeof(max_align_t)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
alignas(TINYMALLOC_MAX_ALIGN) static struct {
|
static struct {
|
||||||
char memory[TINYMALLOC_MAX_BYTES];
|
alignas(max_align_t) char bits[TINYMALLOC_MAX_BYTES];
|
||||||
size_t used, last, free;
|
char *memory;
|
||||||
|
int once;
|
||||||
|
size_t size, used, last, free;
|
||||||
} heap;
|
} 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;
|
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) {
|
void *memalign(size_t align, size_t need) {
|
||||||
char *res;
|
char *res;
|
||||||
size_t next, next2, base, toto, *link, *link2;
|
size_t next, next2, base, toto, *link, *link2;
|
||||||
|
tinymalloc_init();
|
||||||
|
|
||||||
// normalize arguments
|
// normalize arguments
|
||||||
while (align & (align - 1))
|
while (align & (align - 1))
|
||||||
|
@ -95,7 +111,7 @@ void *memalign(size_t align, size_t need) {
|
||||||
base &= -align;
|
base &= -align;
|
||||||
if (ckd_add(&toto, base, need))
|
if (ckd_add(&toto, base, need))
|
||||||
goto OutOfMemory;
|
goto OutOfMemory;
|
||||||
if (toto > TINYMALLOC_MAX_BYTES)
|
if (toto > heap.size)
|
||||||
goto OutOfMemory;
|
goto OutOfMemory;
|
||||||
res = heap.memory + base;
|
res = heap.memory + base;
|
||||||
((size_t *)res)[-1] = need;
|
((size_t *)res)[-1] = need;
|
||||||
|
@ -148,7 +164,7 @@ void *realloc(void *ptr, size_t need) {
|
||||||
need &= -sizeof(size_t);
|
need &= -sizeof(size_t);
|
||||||
if (ckd_add(&toto, base, need))
|
if (ckd_add(&toto, base, need))
|
||||||
goto OutOfMemory;
|
goto OutOfMemory;
|
||||||
if (toto > TINYMALLOC_MAX_BYTES)
|
if (toto > heap.size)
|
||||||
goto OutOfMemory;
|
goto OutOfMemory;
|
||||||
((size_t *)mem)[-1] = need;
|
((size_t *)mem)[-1] = need;
|
||||||
heap.used = toto;
|
heap.used = toto;
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include "libc/fmt/itoa.h"
|
#include "libc/fmt/itoa.h"
|
||||||
#include "libc/intrin/describeflags.internal.h"
|
#include "libc/intrin/describeflags.internal.h"
|
||||||
#include "libc/intrin/dll.h"
|
#include "libc/intrin/dll.h"
|
||||||
|
#include "libc/intrin/kprintf.h"
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
#include "libc/macros.internal.h"
|
#include "libc/macros.internal.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
|
@ -158,6 +159,7 @@ static const char *stubpath;
|
||||||
static long FLAG_SizeOfStackCommit = 64 * 1024;
|
static long FLAG_SizeOfStackCommit = 64 * 1024;
|
||||||
static long FLAG_SizeOfStackReserve = 8 * 1024 * 1024;
|
static long FLAG_SizeOfStackReserve = 8 * 1024 * 1024;
|
||||||
|
|
||||||
|
#define TINYMALLOC_MAX_ALIGN MAX_ALIGN
|
||||||
#include "libc/mem/tinymalloc.inc"
|
#include "libc/mem/tinymalloc.inc"
|
||||||
|
|
||||||
static wontreturn void Die(const char *thing, const char *reason) {
|
static wontreturn void Die(const char *thing, const char *reason) {
|
||||||
|
@ -186,6 +188,13 @@ static void *Calloc(size_t n) {
|
||||||
return p;
|
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) {
|
static void *Realloc(void *p, size_t n) {
|
||||||
if (!(p = realloc(p, n)))
|
if (!(p = realloc(p, n)))
|
||||||
DieOom();
|
DieOom();
|
||||||
|
@ -1106,7 +1115,7 @@ int main(int argc, char *argv[]) {
|
||||||
GetOpts(argc, argv);
|
GetOpts(argc, argv);
|
||||||
// translate executable
|
// translate executable
|
||||||
struct Elf *elf = OpenElf(argv[optind]);
|
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);
|
struct ImagePointer ip = GeneratePe(elf, buf, 0x00400000);
|
||||||
if (creat(outpath, 0755) == -1)
|
if (creat(outpath, 0755) == -1)
|
||||||
DieSys(elf->path);
|
DieSys(elf->path);
|
||||||
|
|
Loading…
Reference in a new issue