Skip to content

Commit

Permalink
Merge pull request #4635 from AidanHa/elf
Browse files Browse the repository at this point in the history
Add Data Section and Header to Relocatable ELF Object
  • Loading branch information
0xdaryl authored Dec 19, 2019
2 parents e6f60a1 + 35eaa4d commit 3634948
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
52 changes: 48 additions & 4 deletions compiler/codegen/ELFGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ TR::ELFGenerator::initializeTextSection(uint32_t shName, ELFAddress shAddress,
_textSection = shdr;
strcpy(_textSectionName, ".text");
}

void
TR::ELFGenerator::initializeDataSection(uint32_t shName, ELFAddress shAddress,
ELFOffset shOffset, uint32_t shSize)
{

ELFSectionHeader * shdr = static_cast<ELFSectionHeader *>(_rawAllocator.allocate(sizeof(ELFSectionHeader)));

shdr->sh_name = shName;
shdr->sh_type = SHT_PROGBITS;
shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
shdr->sh_addr = shAddress;
shdr->sh_offset = shOffset;
shdr->sh_size = shSize;
shdr->sh_link = 0;
shdr->sh_info = 0;
shdr->sh_addralign = 8;
shdr->sh_entsize = 0;

_dataSection = shdr;
strcpy(_dataSectionName, ".data");
}

void
TR::ELFGenerator::initializeDynSymSection(uint32_t shName, ELFOffset shOffset, uint32_t shSize, uint32_t shLink)
Expand Down Expand Up @@ -227,11 +249,15 @@ TR::ELFGenerator::emitELFFile(const char * filename)
}

writeCodeSegmentToFile(elfFile);

writeDataSegmentToFile(elfFile);

writeSectionHeaderToFile(elfFile, _zeroSection);

writeSectionHeaderToFile(elfFile, _textSection);

writeSectionHeaderToFile(elfFile, _dataSection);

if (_relaSection)
{
writeSectionHeaderToFile(elfFile, _relaSection);
Expand All @@ -246,6 +272,9 @@ TR::ELFGenerator::emitELFFile(const char * filename)
writeSectionNameToFile(elfFile, _zeroSectionName, sizeof(_zeroSectionName));

writeSectionNameToFile(elfFile, _textSectionName, sizeof(_textSectionName));

writeSectionNameToFile(elfFile, _dataSectionName, sizeof(_dataSectionName));

if (_relaSection)
{
writeSectionNameToFile(elfFile, _relaSectionName, sizeof(_relaSectionName));
Expand Down Expand Up @@ -298,6 +327,14 @@ TR::ELFGenerator::writeCodeSegmentToFile(::FILE *fp)
fwrite(static_cast<const void *>(_codeStart), sizeof(uint8_t), _codeSize, fp);
}

void
TR::ELFGenerator::writeDataSegmentToFile(::FILE *fp)
{
//char temp[] = "Hello World";
//fwrite(static_cast<const void *>(temp), sizeof(uint8_t), sizeof(temp), fp);
return;
}

void
TR::ELFGenerator::writeELFSymbolsToFile(::FILE *fp)
{
Expand Down Expand Up @@ -509,8 +546,8 @@ TR::ELFRelocatableGenerator::initializeELFHeader(void)
_header->e_shoff = sizeof(ELFEHeader) + _codeSize; //start of the section header table in bytes from the first byte of the ELF file
_header->e_phentsize = 0; //no program headers in relocatable elf
_header->e_phnum = 0;
_header->e_shnum = 6;
_header->e_shstrndx = 4; //index of section header string table
_header->e_shnum = 7;
_header->e_shstrndx = 5; //index of section header string table
}

void
Expand All @@ -519,14 +556,15 @@ TR::ELFRelocatableGenerator::buildSectionHeaders(void)
uint32_t shStrTabNameLength = sizeof(_zeroSectionName) +
sizeof(_shStrTabSectionName) +
sizeof(_textSectionName) +
sizeof(_dataSectionName) +
sizeof(_relaSectionName) +
sizeof(_dynSymSectionName) +
sizeof(_dynStrSectionName);

/* offset calculations */
uint32_t trailerStartOffset = sizeof(ELFEHeader) + _codeSize;
uint32_t symbolsStartOffset = trailerStartOffset +
(sizeof(ELFSectionHeader) * /* # shdr */ 6) +
(sizeof(ELFSectionHeader) * /* # shdr */ 7) +
shStrTabNameLength;
uint32_t symbolNamesStartOffset = symbolsStartOffset +
(_numSymbols + /* UNDEF */ 1) * sizeof(ELFSymbol);
Expand All @@ -542,6 +580,12 @@ TR::ELFRelocatableGenerator::buildSectionHeaders(void)
_codeSize);
shNameOffset += sizeof(_textSectionName);

initializeDataSection(shNameOffset,
/*sh_addr*/0,
sizeof(ELFEHeader) + _codeSize,
0);
shNameOffset += sizeof(_dataSectionName);

initializeRelaSection(shNameOffset,
relaStartOffset,
_numRelocations * sizeof(ELFRela));
Expand All @@ -550,7 +594,7 @@ TR::ELFRelocatableGenerator::buildSectionHeaders(void)
initializeDynSymSection(shNameOffset,
symbolsStartOffset,
symbolNamesStartOffset - symbolsStartOffset,
/*Index of dynStrTab*/ 5);
/*Index of dynStrTab*/ 6);
shNameOffset += sizeof(_dynSymSectionName);

initializeStrTabSection(shNameOffset,
Expand Down
25 changes: 24 additions & 1 deletion compiler/codegen/ELFGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ELFGenerator
_programHeader(NULL),
_zeroSection(NULL),
_textSection(NULL),
_dataSection(NULL),
_relaSection(NULL),
_dynSymSection(NULL),
_shStrTabSection(NULL),
Expand Down Expand Up @@ -139,6 +140,20 @@ class ELFGenerator
uint32_t shSize
);

/**
* Set up the trailer data section
* @param[in] shName the section header name
* @param[in] shAddress the section header address
* @param[in] shOffset the section header offset
* @param[in] shSize the section header size
*/
void initializeDataSection(
uint32_t shName,
ELFAddress shAddress,
ELFOffset shOffset,
uint32_t shSize
);

/**
* Set up the trailer dynamic symbol section
* @param[in] shName the section header name
Expand Down Expand Up @@ -234,6 +249,12 @@ class ELFGenerator
*/
void writeCodeSegmentToFile(::FILE *fp);

/**
* Write the code segment to file
* @param[in] fp the file stream ptr
*/
void writeDataSegmentToFile(::FILE *fp);

/**
* Write the ELFSymbols to file
* @param[in] fp the file stream ptr
Expand All @@ -255,7 +276,9 @@ class ELFGenerator
ELFSectionHeader *_zeroSection;
char _zeroSectionName[1];
ELFSectionHeader *_textSection;
char _textSectionName[6];
char _textSectionName[6];
ELFSectionHeader *_dataSection;
char _dataSectionName[6];
ELFSectionHeader *_relaSection;
char _relaSectionName[11];
ELFSectionHeader *_dynSymSection;
Expand Down

0 comments on commit 3634948

Please sign in to comment.