ELFBinaryReader.cpp revision d0fbbb227051be16931a1aa9b4a7722ac039c698
1//===- ELFBinaryReader.cpp ------------------------------------------------===// 2// 3// The MCLinker Project 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9#include <mcld/LD/ELFBinaryReader.h> 10 11#include <mcld/IRBuilder.h> 12#include <mcld/LinkerConfig.h> 13#include <mcld/MC/MCLDInput.h> 14#include <mcld/Support/MemoryArea.h> 15#include <mcld/Target/GNULDBackend.h> 16 17#include <llvm/Support/ELF.h> 18 19using namespace mcld; 20 21//===----------------------------------------------------------------------===// 22// ELFBinaryReader 23//===----------------------------------------------------------------------===// 24/// constructor 25ELFBinaryReader::ELFBinaryReader(GNULDBackend& pBackend, 26 IRBuilder& pBuilder, 27 const LinkerConfig& pConfig) 28 : BinaryReader(), 29 m_Backend(pBackend), 30 m_Builder(pBuilder), 31 m_Config(pConfig) { 32} 33 34/// destructor 35ELFBinaryReader::~ELFBinaryReader() 36{ 37} 38 39bool ELFBinaryReader::readBinary(Input& pInput) 40{ 41 // section: NULL 42 m_Builder.CreateELFHeader(pInput, 43 "", 44 LDFileFormat::Null, 45 llvm::ELF::SHT_NULL, 46 0x0); 47 48 // section: .data 49 LDSection* data_sect = 50 m_Builder.CreateELFHeader(pInput, 51 ".data", 52 LDFileFormat::Regular, 53 llvm::ELF::SHF_WRITE | llvm::ELF::SHF_ALLOC, 54 0x1); 55 56 57 SectionData* data = m_Builder.CreateSectionData(*data_sect); 58 size_t data_size = pInput.memArea()->handler()->size(); 59 Fragment* frag = m_Builder.CreateRegion(pInput, 0x0, data_size); 60 m_Builder.AppendFragment(*frag, *data); 61 62 // section: .shstrtab 63 m_Builder.CreateELFHeader(pInput, 64 ".shstrtab", 65 LDFileFormat::NamePool, 66 llvm::ELF::SHT_STRTAB, 67 0x1); 68 69 // section: .symtab 70 m_Builder.CreateELFHeader(pInput, 71 ".symtab", 72 LDFileFormat::NamePool, 73 llvm::ELF::SHT_SYMTAB, 74 m_Config.targets().bitclass() / 8); 75 76 // symbol: .data 77 m_Builder.AddSymbol(pInput, 78 ".data", 79 ResolveInfo::Section, 80 ResolveInfo::Define, 81 ResolveInfo::Local, 82 0x0, 83 0x0, 84 data_sect); 85 86 // symbol: _start 87 m_Builder.AddSymbol(pInput, 88 "_binary_" + pInput.path().filename().string() + "_start", 89 ResolveInfo::NoType, 90 ResolveInfo::Define, 91 ResolveInfo::Global, 92 0x0, 93 0x0, 94 data_sect); 95 96 // symbol: _end 97 m_Builder.AddSymbol(pInput, 98 "_binary_" + pInput.path().filename().string() + "_end", 99 ResolveInfo::NoType, 100 ResolveInfo::Define, 101 ResolveInfo::Global, 102 0x0, 103 data_size, 104 data_sect); 105 106 // symbol: _size 107 m_Builder.AddSymbol(pInput, 108 "_binary_" + pInput.path().filename().string() + "_size", 109 ResolveInfo::NoType, 110 ResolveInfo::Define, 111 ResolveInfo::Global, 112 0x0, 113 data_size, 114 data_sect); 115 116 // section: .strtab 117 m_Builder.CreateELFHeader(pInput, 118 ".strtab", 119 LDFileFormat::NamePool, 120 llvm::ELF::SHT_STRTAB, 121 0x1); 122 123 return true; 124} 125