Rewrite Cosmopolitan Ar

The build/bootstrap/ar.com program is now tinier. This change reduces
its size from 140kb to 53kb. Nothing was traded away. Cosmopolitan Ar
performance is now 2x better than llvm-ar largely thanks to using the
copy_file_range() system call. This change homebrews a new allocation
API that addresses the shortcomings of the C standard library design.
Using these new balloc() and reballoc() functions I managed to reduce
memory consumption so much that Cosmpolitan Ar should now use roughly
100x fewer bytes of peak resident memory compared to llvm-ar. Correct
behavior with better compatibility has been assured. Binary output is
now pretty much bit-identical to llvm-ar, as of this change. This can
and should be the living proof we need to show that a better world is
possible for software.
This commit is contained in:
Justine Tunney 2023-07-02 10:19:16 -07:00
parent 197aa0d465
commit 0c630d95b5
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
27 changed files with 916 additions and 341 deletions

View file

@ -24,21 +24,32 @@
/**
* Returns `strtab + i` from elf string table.
*
* @param elf points to the start of the executable image
* @param elf points to the start of the executable image data
* @param mapsize is the number of bytes past `elf` we can access
* @param strtab is double-nul string list from GetElfStringTable()
* @param i is byte index into strtab where needed string starts
* @return pointer to nul terminated string, or null on error
* which may be null, in which case only the `!i` name is valid
* @param i is byte index into strtab where needed string starts or
* zero (no name) in which case empty string is always returned
* as a pointer to the read-only string literal, rather than in
* the elf image, since the elf spec permits an empty or absent
* string table section
* @return a const nul-terminated string pointer, otherwise null if
* 1. `i` was nonzero and `strtab` was null, or
* 2. `strtab+i` wasn't inside `[elf,elf+mapsize)`, or
* 3. a nul byte wasn't present within `[strtab+i,elf+mapsize)`, or
* 4. an arithmetic overflow occurred
*/
char *GetElfString(const Elf64_Ehdr *elf, // validated
size_t mapsize, // validated
const char *strtab, // validated
Elf64_Word i) { // foreign
const char *GetElfString(const Elf64_Ehdr *elf, // validated
size_t mapsize, // validated
const char *strtab, // validated
Elf64_Word i) { // foreign
const char *e;
if (!i) return "";
e = (const char *)elf;
if (!strtab) return 0;
if (strtab < e) return 0;
if (strtab >= e + mapsize) return 0;
if (strtab + i >= e + mapsize) return 0;
if (!memchr(strtab + i, 0, (e + mapsize) - (strtab + i))) return 0;
return (char *)strtab + i;
return (const char *)strtab + i;
}