Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ld-tigcc: Add support for importing ELF object files #18

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions trunk/tigcc/doc/System/Info/ld/formats.hss
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ each format is described in the following table:
<TD VALIGN="TOP">Yes (through symbols)</TD>
</TR>
<TR>
<TD VALIGN="TOP"><B>ELF</B></TD>
<TD VALIGN="TOP">Yes</TD>
<TD VALIGN="TOP">Yes</TD>
<TD VALIGN="TOP">Yes</TD>
<TD VALIGN="TOP">Yes</TD>
<TD VALIGN="TOP">Yes (through unresolved relocations)</TD>
<TD VALIGN="TOP">Yes (through unresolved relocations)</TD>
<TD VALIGN="TOP">Yes (through unresolved relocations)</TD>
<TD VALIGN="TOP">Yes (through symbols)</TD>
<TD VALIGN="TOP">Yes (but not yet supported by the linker)</TD>
<TD VALIGN="TOP">Yes (through symbols)</TD>
<TD VALIGN="TOP">Yes (through symbols)</TD>
</TR>
<TR>
<TD VALIGN="TOP"><B>TIOS ASM</B></TD>
<TD VALIGN="TOP">No</TD>
<TD VALIGN="TOP">4-byte absolute only</TD>
Expand Down
6 changes: 3 additions & 3 deletions trunk/tigcc/ld-tigcc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ endif
CC ?= gcc
CFLAGS = -s -Os -fno-exceptions
WARN_CFLAGS = -W -Wall -Wwrite-strings -Wpointer-arith
DEFINES = -DCOFF_SUPPORT -DAMIGAOS_SUPPORT -DTIOS_SUPPORT -DFLASH_OS_SUPPORT -DNOSTUB_DLL_SUPPORT -DFARGO_SUPPORT -DDATA_VAR_SUPPORT -DTIOS_FILE_SUPPORT -DTIOS_UPGRADE_FILE_SUPPORT -DDEBUGGING_INFO_SUPPORT -DCOFF_TIGCC_EXTENSIONS -DAMIGAOS_TIGCC_EXTENSIONS
DEFINES = -DCOFF_SUPPORT -DAMIGAOS_SUPPORT -DELF_SUPPORT -DTIOS_SUPPORT -DFLASH_OS_SUPPORT -DNOSTUB_DLL_SUPPORT -DFARGO_SUPPORT -DDATA_VAR_SUPPORT -DTIOS_FILE_SUPPORT -DTIOS_UPGRADE_FILE_SUPPORT -DDEBUGGING_INFO_SUPPORT -DCOFF_TIGCC_EXTENSIONS -DAMIGAOS_TIGCC_EXTENSIONS
EXE_DEFINES = -DPUCRUNCH_SUPPORT -DENABLE_HELP -DENABLE_STATS -DENABLE_DUMP

RM = rm
Expand All @@ -31,8 +31,8 @@ COMPILE_C = $(CC) -c $(CFLAGS) $(ARCHFLAGS) $(WARN_CFLAGS) $(DEFINES)

HEADERS = *.h formats/*.h import/*.h export/*.h bincode/*.h insert/*.h insert/model/*.h int_def.inc
AR_HEADERS = ar/*.h ar/import/*.h ar/export/*.h *.h formats/*.h
OBJECTS = main.o integers.o int_arb.o manip.o constmrg.o gcunused.o reorder.o import/import.o import/imp_coff.o import/imp_amig.o import/imp_ar.o export/export.o export/exp_def.o export/exp_os.o export/exp_tios.o export/exp_ndll.o export/exp_farg.o export/exp_data.o export/exp_dbg.o export/pucrunch.o bincode/fix_m68k.o bincode/fix_tios.o bincode/fix_emu.o bincode/cutrange.o insert/ins_def.o insert/model/list.o insert/kernel.o insert/comprrlc.o insert/other.o special.o dump.o
AR_OBJECTS = ar/main.o ar/manip.o ar/import/import.o ar/import/imp_coff.o ar/import/imp_amig.o ar/export/exp_ar.o ar/dump.o integers.o
OBJECTS = main.o integers.o int_arb.o manip.o constmrg.o gcunused.o reorder.o import/import.o import/imp_coff.o import/imp_amig.o import/imp_elf.o import/imp_ar.o export/export.o export/exp_def.o export/exp_os.o export/exp_tios.o export/exp_ndll.o export/exp_farg.o export/exp_data.o export/exp_dbg.o export/pucrunch.o bincode/fix_m68k.o bincode/fix_tios.o bincode/fix_emu.o bincode/cutrange.o insert/ins_def.o insert/model/list.o insert/kernel.o insert/comprrlc.o insert/other.o special.o dump.o
AR_OBJECTS = ar/main.o ar/manip.o ar/import/import.o ar/import/imp_coff.o ar/import/imp_amig.o ar/import/imp_elf.o ar/export/exp_ar.o ar/dump.o integers.o
DLL_OBJECTS = main.do integers.do int_arb.do manip.do constmrg.do gcunused.do reorder.do import/import.do import/imp_coff.do import/imp_amig.do import/imp_ar.do export/export.do export/exp_def.do export/exp_os.do export/exp_tios.do export/exp_ndll.do export/exp_farg.do export/exp_data.do export/exp_dbg.do bincode/fix_m68k.do bincode/fix_tios.do bincode/fix_emu.do bincode/cutrange.do insert/ins_def.do insert/model/list.do insert/kernel.do insert/comprrlc.do insert/other.do special.do
DLL_AR_OBJECTS = ar/main.do ar/manip.do ar/import/import.do ar/import/imp_coff.do ar/import/imp_amig.do ar/export/exp_ar.do
BACKUPS = *~ format/*~ import/*~ export/*~ bincode/*~ insert/*~ insert/model/*~ ar/*~ ar/import/*~ ar/export/*~
Expand Down
184 changes: 184 additions & 0 deletions trunk/tigcc/ld-tigcc/ar/import/imp_elf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/* imp_elf.c: Routines to import an ELF file

Copyright (C) 2024 Peter Lafreniere <[email protected]>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#ifdef ELF_SUPPORT

#include "imp_elf.h"

#include "../../formats/elf.h"

#include <stdlib.h>
#include <string.h>


BOOLEAN ArImportELFFile (OBJECT_FILE *ObjectFile)
{

// Call this for a nice and clean failing exit.
#define FailMsg(msg) ({ Error (ObjectFile->FileName, msg); return FALSE; })
#define Fail() FailMsg("Corrupt ELF object file.")
#define TestMem(Ptr) ({ if (!(Ptr)) FailMsg("Out of memory."); })

// Check if a given object with a given type is completely inside the file.
#define IsInFile(Ptr,Type) (((const I1 *) (Ptr)) >= ObjectFile->Data && ((const I1 *) (Ptr)) + sizeof (Type) <= ObjectFile->Data + ObjectFile->Size)
#define TestInFile(Ptr,Type) ({ if (!(IsInFile (Ptr, Type))) { Fail (); } })

const ELF_HEADER *ELFHeader = (ELF_HEADER *)ObjectFile->Data;
const ELF_SYMBOLS *ELFSymbols = NULL;
const ELF_SECTIONS *ELFSections;
const char *SymNames = NULL;
const char *SecNames;
SIZE SecNamesSize;
SIZE SymNamesSize;
COUNT SecCount;
COUNT SymCount;
OFFSET SecIdx;
OFFSET SymIdx;

if (!IsValidELFObjectFile (ELFHeader))
FailMsg ("Invalid ELF object file");

ELFSections = (ELF_SECTIONS *)(ObjectFile->Data + ReadTI4 (ELFHeader->SectionHeadOff));
SecCount = ReadTI2 (ELFHeader->SectionHeadCount);
if (IsZero (SecCount))
Fail();

// *** Initialize section string table data ***
{
OFFSET SectionNameIdx = ReadTI2 (ELFHeader->SectionNameIdx);
const ELF_SECTION *Section;

if (IsZero (SectionNameIdx)) {
Fail ();
} else if (SectionNameIdx == 0xFFFF) {
Section = &(*ELFSections) [0];
TestInFile (Section, ELF_SECTION);
SectionNameIdx = ReadTI4 (Section->Link);
}

Section = &(*ELFSections) [SectionNameIdx];
TestInFile (Section, ELF_SECTION);

SecNames = (char *)(ObjectFile->Data + ReadTI4 (Section->FileOffset));
SecNamesSize = ReadTI4 (Section->Size);

// Bounds check the string table here rather than for each string
// And make sure that the section is null-terminated
if (SecNames + SecNamesSize > (char *)ObjectFile->Data + ObjectFile->Size ||
!IsZero (SecNames [SecNamesSize - 1]))
Fail ();
}

// *** Locate symbol table and symbol string data ***
for (SecIdx = 0; SecIdx < SecCount; SecIdx++) {
const ELF_SECTION *Section;
const char *Name;
I4 Type;

Section = &(*ELFSections) [SecIdx];
TestInFile (Section, ELF_SECTION);

Type = ReadTI4 (Section->Type);

// If this is the symbol table
if (Type == SHT_SYMTAB) {
if (ELFSymbols != NULL)
FailMsg ("Multiple ELF symbol tables found");

// Make sure that the symbols are the right format
if (ReadTI4 (Section->EntrySize) != sizeof (ELF_SYMBOL))
Fail ();

ELFSymbols = (ELF_SYMBOLS *)(ObjectFile->Data + ReadTI4 (Section->FileOffset));
SymCount = ReadTI4 (Section->Size) / sizeof (ELF_SYMBOL);
continue;
}

// Bounds check the name
if ((SIZE) ReadTI4 (Section->NameIdx) > SecNamesSize)
Fail ();

Name = &SecNames [ReadTI4 (Section->NameIdx)];

// If this is the symbol string table
if (Type == SHT_STRTAB && !strncmp (Name, ".strtab", 7)) {
SymNames = (char *)(ObjectFile->Data + ReadTI4 (Section->FileOffset));
SymNamesSize = ReadTI4 (Section->Size);

// Bounds check the string table here rather than for each string
// And make sure that the section is null-terminated
if (SymNames + SymNamesSize > (char *)ObjectFile->Data + ObjectFile->Size ||
!IsZero (SymNames [SymNamesSize - 1]))
Fail ();
}
}

if (ELFSymbols == NULL || SymNames == NULL)
Fail ();

// For each symbol
for (SymIdx = 0; SymIdx < SymCount; SymIdx++) {
const ELF_SYMBOL *ELFSymbol;
const char *Name;
OFFSET SymSecIdx;

ELFSymbol = &(*ELFSymbols) [SymIdx];
// We've already checked this for every symbol in the table
//TestInFile(ELFSymbol, ELF_SYMBOL);

// Bounds check the name
if ((SIZE) ReadTI4 (ELFSymbol->NameIdx) > SymNamesSize)
Fail ();

// If the symbol has no name, this should point to a null byte
Name = &SymNames [ReadTI4 (ELFSymbol->NameIdx)];

SymSecIdx = ReadTI2 (ELFSymbol->SectionIdx);

if (!IsZero (*Name) && !IsZero (SymSecIdx) && SymSecIdx < SecCount) {
char Visibility;
char Bind;

Visibility = ReadTI1 (ELFSymbol->Visibility);
Bind = ELF32_ST_BIND (ReadTI1 (ELFSymbol->Info));

// If the symbol is exported, emit it
//
// Note that all visibilities are treated as default when linking,
// which can lead to unexpected results.
//
// The same applies to weak binding.
if (Visibility != STV_HIDDEN && (Bind == STB_GLOBAL || Bind == STB_WEAK)) {
SYMBOL *ARSym = calloc (1, sizeof (SYMBOL));
TestMem (ARSym);

ARSym->Parent = ObjectFile;

ARSym->NameLength = strnlen (Name, MAX_SYM_LEN);
memcpy (ARSym->Name, Name, ARSym->NameLength);

Append (ObjectFile->Symbols, ARSym);
ObjectFile->Parent->SymbolCount++;
}
}
}

return TRUE;
}

#endif /* ELF_SUPPORT */
33 changes: 33 additions & 0 deletions trunk/tigcc/ld-tigcc/ar/import/imp_elf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* imp_elf.h: Routines to import an ELF file

Copyright (C) 2024 Peter Lafreniere <[email protected]>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#ifndef IMP_ELF_H
#define IMP_ELF_H

#include "../../generic.h"

#ifdef ELF_SUPPORT

#include "../data.h"

// Import the exported symbols of an m68k-elf file.
BOOLEAN ArImportELFFile (OBJECT_FILE *ObjectFile);

#endif /* ELF_SUPPORT */

#endif /* IMP_ELF_H */
10 changes: 10 additions & 0 deletions trunk/tigcc/ld-tigcc/ar/import/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#include "imp_amig.h"
#endif /* AMIGAOS_SUPPORT */

#ifdef ELF_SUPPORT
#include "../../formats/elf.h"
#include "imp_elf.h"
#endif /* ELF_SUPPORT */

// Import an object file with an arbitrary format.
BOOLEAN ArImportObjectFile (OBJECT_FILE *ObjectFile)
{
Expand All @@ -44,6 +49,11 @@ BOOLEAN ArImportObjectFile (OBJECT_FILE *ObjectFile)
return (ArImportAmigaOSFile (ObjectFile));
else
#endif /* AMIGAOS_SUPPORT */
#ifdef ELF_SUPPORT
if (IsELFFile (ObjectFile->Data, ObjectFile->Size))
return (ArImportELFFile (ObjectFile));
else
#endif /* ELF_SUPPORT */
{
Warning (ObjectFile->FileName, "Unknown object file format.");
return FALSE;
Expand Down
Loading