Skip to content

Commit f25beb3

Browse files
committed
Add architecture flag to zipobj
1 parent bed7718 commit f25beb3

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed

third_party/chibicc/as.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3944,7 +3944,7 @@ static void Objectify(struct As *a, int path) {
39443944
char *p;
39453945
int i, j, s, e;
39463946
struct ElfWriter *elf;
3947-
elf = elfwriter_open(a->strings.p[path], 0644);
3947+
elf = elfwriter_open(a->strings.p[path], 0644, EM_NEXGEN32E);
39483948
for (i = 0; i < a->symbols.n; ++i) {
39493949
if (!IsLiveSymbol(a, i)) continue;
39503950
p = strndup(a->slices.p[a->symbols.p[i].name].p,

third_party/python/pyobj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ Objectify(void)
646646
memcpy(pycdata + sizeof(header), mardata, marsize);
647647
yoinked = newinterner();
648648
forcepulls = newinterner();
649-
elf = elfwriter_open(outpath, 0644);
649+
elf = elfwriter_open(outpath, 0644, 0);
650650
elfwriter_cargoculting(elf);
651651
if (ispkg) {
652652
elfwriter_zip(elf, zipdir, zipdir, strlen(zipdir),

tool/build/lib/elfwriter.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static void FlushTables(struct ElfWriter *elf) {
156156
elfwriter_commit(elf, size);
157157
}
158158

159-
struct ElfWriter *elfwriter_open(const char *path, int mode) {
159+
struct ElfWriter *elfwriter_open(const char *path, int mode, int arch) {
160160
struct ElfWriter *elf;
161161
CHECK_NOTNULL((elf = calloc(1, sizeof(struct ElfWriter))));
162162
CHECK_NOTNULL((elf->path = strdup(path)));
@@ -166,15 +166,22 @@ struct ElfWriter *elfwriter_open(const char *path, int mode) {
166166
elf->mapsize, PROT_READ | PROT_WRITE,
167167
MAP_SHARED | MAP_FIXED, elf->fd, 0)));
168168
elf->ehdr = memcpy(elf->map, &kObjHeader, (elf->wrote = sizeof(kObjHeader)));
169-
if (strstr(path, "/aarch64")) {
170-
elf->ehdr->e_machine = EM_AARCH64;
171-
} else if (strstr(path, "/powerpc64")) {
172-
elf->ehdr->e_machine = EM_PPC64;
173-
} else if (strstr(path, "/riscv")) {
174-
elf->ehdr->e_machine = EM_RISCV;
175-
} else if (strstr(path, "/s390")) {
169+
if (!arch) {
170+
#ifdef __x86_64__
171+
arch = EM_NEXGEN32E;
172+
#elif defined(__aarch64__)
173+
arch = EM_AARCH64;
174+
#elif defined(__powerpc64__)
175+
arch = EM_PPC64;
176+
#elif defined(__riscv)
177+
arch = EM_RISCV;
178+
#elif defined(__s390x__)
176179
elf->ehdr->e_machine = EM_S390;
180+
#else
181+
#error "unsupported architecture"
182+
#endif
177183
}
184+
elf->ehdr->e_machine = arch;
178185
elf->strtab = newinterner();
179186
elf->shstrtab = newinterner();
180187
intern(elf->strtab, "");

tool/build/lib/elfwriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct ElfWriter {
5454
struct Interner *shstrtab;
5555
};
5656

57-
struct ElfWriter *elfwriter_open(const char *, int) __wur;
57+
struct ElfWriter *elfwriter_open(const char *, int, int) __wur;
5858
void elfwriter_cargoculting(struct ElfWriter *);
5959
void elfwriter_close(struct ElfWriter *);
6060
void elfwriter_align(struct ElfWriter *, size_t, size_t);

tool/build/zipobj.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "tool/build/lib/elfwriter.h"
4444
#include "tool/build/lib/stripcomponents.h"
4545

46+
int arch_;
4647
char *name_;
4748
char *yoink_;
4849
char *symbol_;
@@ -76,6 +77,7 @@ FLAGS\n\
7677
-o PATH output path\n\
7778
-0 disable compression\n\
7879
-B basename-ify zip filename\n\
80+
-a ARCH microprocessor architecture\n\
7981
-N ZIPPATH zip filename (defaults to input arg)\n\
8082
-P ZIPPATH prepend path zip filename using join\n\
8183
-C INTEGER strips leading path components from zip filename\n\
@@ -86,14 +88,34 @@ FLAGS\n\
8688
exit(rc);
8789
}
8890

91+
static int ParseArch(const char *s) {
92+
if (startswith(s, "x86")) {
93+
return EM_NEXGEN32E;
94+
} else if (startswith(s, "arm") || !strcmp(s, "aarch64")) {
95+
return EM_AARCH64;
96+
} else if (startswith(s, "ppc") || startswith(s, "powerpc")) {
97+
return EM_PPC64;
98+
} else if (startswith(s, "riscv")) {
99+
return EM_RISCV;
100+
} else if (startswith(s, "s390")) {
101+
return EM_S390;
102+
} else {
103+
tinyprint(2, "error: unrecognized microprocessor architecture\n", NULL);
104+
exit(1);
105+
}
106+
}
107+
89108
void GetOpts(int *argc, char ***argv) {
90109
int opt;
91110
yoink_ = "__zip_eocd";
92-
while ((opt = getopt(*argc, *argv, "?0nhBN:C:P:o:s:y:")) != -1) {
111+
while ((opt = getopt(*argc, *argv, "?0nhBN:C:P:o:s:y:a:")) != -1) {
93112
switch (opt) {
94113
case 'o':
95114
outpath_ = optarg;
96115
break;
116+
case 'a':
117+
arch_ = ParseArch(optarg);
118+
break;
97119
case 'n':
98120
exit(0);
99121
case 's':
@@ -192,26 +214,25 @@ void PullEndOfCentralDirectoryIntoLinkage(struct ElfWriter *elf) {
192214
}
193215

194216
void CheckFilenameKosher(const char *path) {
195-
CHECK_LE(kZipCfileHdrMinSize + strlen(path), 65535);
196-
CHECK(!startswith(path, "/"));
197-
CHECK(!strstr(path, ".."));
217+
unassert(kZipCfileHdrMinSize + strlen(path) <= 65535);
218+
unassert(!startswith(path, "/"));
219+
unassert(!strstr(path, ".."));
198220
}
199221

200222
void zipobj(int argc, char **argv) {
201223
size_t i;
202224
struct ElfWriter *elf;
203-
CHECK_LT(argc, UINT16_MAX / 3 - 64); /* ELF 64k section limit */
225+
unassert(argc < UINT16_MAX / 3 - 64); /* ELF 64k section limit */
204226
GetOpts(&argc, &argv);
205227
for (i = 0; i < argc; ++i) CheckFilenameKosher(argv[i]);
206-
elf = elfwriter_open(outpath_, 0644);
228+
elf = elfwriter_open(outpath_, 0644, arch_);
207229
elfwriter_cargoculting(elf);
208230
for (i = 0; i < argc; ++i) ProcessFile(elf, argv[i]);
209231
PullEndOfCentralDirectoryIntoLinkage(elf);
210232
elfwriter_close(elf);
211233
}
212234

213235
int main(int argc, char **argv) {
214-
ShowCrashReports();
215236
timestamp.tv_sec = 1647414000; /* determinism */
216237
/* clock_gettime(CLOCK_REALTIME, &timestamp); */
217238
zipobj(argc, argv);

0 commit comments

Comments
 (0)