From 400666c81af024b6d16100aba88c2e8e78e8eef8 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sat, 5 Feb 2022 23:12:07 -0500 Subject: rts/linker: Catch archives masquerading as object files Check the file's header to catch static archive bearing the `.o` extension, as may happen on Windows after the Clang refactoring. See #21068 --- rts/linker/LoadArchive.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'rts/linker') diff --git a/rts/linker/LoadArchive.c b/rts/linker/LoadArchive.c index 99e405db22..86fa5cb94b 100644 --- a/rts/linker/LoadArchive.c +++ b/rts/linker/LoadArchive.c @@ -241,7 +241,7 @@ lookupGNUArchiveIndex(int gnuFileIndexSize, char **fileName_, return true; } -static HsInt loadArchive_ (pathchar *path) +HsInt loadArchive_ (pathchar *path) { char *image = NULL; HsInt retcode = 0; @@ -632,3 +632,21 @@ HsInt loadArchive (pathchar *path) RELEASE_LOCK(&linker_mutex); return r; } + +bool isArchive (pathchar *path) +{ + static const char ARCHIVE_HEADER[] = "!\n"; + char buffer[10]; + FILE *f = pathopen(path, WSTR("rb")); + if (f == NULL) { + return false; + } + + size_t ret = fread(buffer, 1, sizeof(buffer), f); + if (ret < sizeof(buffer)) { + return false; + } + fclose(f); + return strncmp(ARCHIVE_HEADER, buffer, sizeof(ARCHIVE_HEADER)-1) == 0; +} + -- cgit v1.2.1