Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions blink/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
#include "blink/endian.h"
#include "blink/machine.h"

#define INTEL "GenuineIntel"
#define BLINK "GenuineBlink"
#define LINUX_ "Linux\0\0\0\0\0\0\0"
#define FREEBSD_ "FreeBSD\0\0\0\0\0\0"
#define NETBSD_ "NetBSD\0\0\0\0\0\0"
#define OPENBSD_ "OpenBSD\0\0\0\0\0"
#define XNU_ "XNU\0\0\0\0\0\0\0\0\0"
#define WINDOWS_ "Windows\0\0\0\0\0"
#define CYGWIN_ "Cygwin\0\0\0\0\0\0"
#define HAIKU_ "Haiku\0\0\0\0\0\0\0"
#define UNKNOWN_ "Unknown\0\0\0\0\0\0"
#define INTEL "GenuineIntel"
#define BLINK "GenuineBlink"
#define LINUX_ "Linux\0\0\0\0\0\0\0"
#define FREEBSD_ "FreeBSD\0\0\0\0\0\0"
#define NETBSD_ "NetBSD\0\0\0\0\0\0"
#define OPENBSD_ "OpenBSD\0\0\0\0\0"
#define DRAGONFLYBSD_ "DragonFlyBSD"
#define XNU_ "XNU\0\0\0\0\0\0\0\0\0"
#define WINDOWS_ "Windows\0\0\0\0\0"
#define CYGWIN_ "Cygwin\0\0\0\0\0\0"
#define HAIKU_ "Haiku\0\0\0\0\0\0\0"
#define UNKNOWN_ "Unknown\0\0\0\0\0\0"

#ifdef __COSMOPOLITAN__
#define OS \
Expand All @@ -49,6 +50,8 @@
#define OS NETBSD_
#elif defined(__OpenBSD__)
#define OS OPENBSD_
#elif defined(__DragonFly__)
#define OS DRAGONFLYBSD_
#elif defined(__APPLE__)
#define OS XNU_
#elif defined(__CYGWIN__)
Expand Down
44 changes: 44 additions & 0 deletions blink/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,47 @@ Elf64_Sym_ *GetElfSymbolTable(const Elf64_Ehdr_ *elf, //
}
return res;
}

const char *GetElfOsNameInNoteTag(const Elf64_Ehdr_ *elf, size_t mapsize) {
int i;
Elf64_Shdr_ *shdr;
const char *sect_name;
const char *note, *note_end;
uint32_t namesz, descsz, type;
const char *os_name;

for (i = 0; i < Read16(elf->shnum); ++i) {
shdr = GetElfSectionHeaderAddress(elf, mapsize, i);
if (!shdr) continue;
if (Read32(shdr->type) != SHT_NOTE_) continue;

sect_name = GetElfSectionName(elf, mapsize, shdr);
if (!sect_name) continue;
if (strcmp(sect_name, ".note.ABI-tag") != 0 &&
strcmp(sect_name, ".note.tag") != 0 &&
strcmp(sect_name, ".note.gnu") != 0)
continue;

note = (const char *)GetElfSectionAddress(elf, mapsize, shdr);
if (!note) continue;
note_end = note + Read64(shdr->size);

while (note + 12 <= note_end) {
namesz = *(const uint32_t *)(note + 0);
descsz = *(const uint32_t *)(note + 4);
type = *(const uint32_t *)(note + 8);

const char *name_field = note + 12;
const char *desc_field = name_field + ((namesz + 3) & ~3);
if (desc_field + ((descsz + 3) & ~3) > note_end) break;

if (namesz && name_field[namesz-1] == '\0') {
return name_field;
}

// Advance to next note
note = desc_field + ((descsz + 3) & ~3);
}
}
return NULL;
}
1 change: 1 addition & 0 deletions blink/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,5 +629,6 @@ char *GetElfSectionNameStringTable(const Elf64_Ehdr_ *, size_t);
void *GetElfSectionAddress(const Elf64_Ehdr_ *, size_t, const Elf64_Shdr_ *);
Elf64_Sym_ *GetElfSymbolTable(const Elf64_Ehdr_ *, size_t, int *);
i64 GetElfMemorySize(const Elf64_Ehdr_ *, size_t, i64 *);
const char *GetElfOsNameInNoteTag(const Elf64_Ehdr_ *, size_t);

#endif /* BLINK_ELF_H_ */
14 changes: 14 additions & 0 deletions blink/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ static bool IsHaikuExecutable(Elf64_Ehdr_ *ehdr, size_t size) {
#endif
}

static bool IsDragonflybsdExecutable(Elf64_Ehdr_ *ehdr, size_t size) {
#ifdef __DragonFly__
const char *name = GetElfOsNameInNoteTag(ehdr, size);
if (name && !strcmp(name, "DragonFly")) {
return true;
}
#endif
return false;
}

static bool IsShebangExecutable(void *image, size_t size) {
return size >= 2 && ((char *)image)[0] == '#' && ((char *)image)[1] == '!';
}
Expand Down Expand Up @@ -342,6 +352,10 @@ bool IsSupportedExecutable(const char *path, void *image, size_t size) {
ExplainWhyItCantBeEmulated(path, "ELF is Haiku executable");
return false;
}
if (IsDragonflybsdExecutable(ehdr, size)) {
ExplainWhyItCantBeEmulated(path, "ELF is DragonFlyBSD executable");
return false;
}
#if defined(__ELF__) && !defined(__linux)
LOGF("blink believes %s is an x86_64-linux executable", path);
#endif
Expand Down