michael@0: // -*- mode: C++ -*- michael@0: michael@0: // Copyright (c) 2011, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Ted Mielczarek michael@0: michael@0: // synth_elf.h: Interface to synth_elf::ELF: fake ELF generator. michael@0: michael@0: #ifndef COMMON_LINUX_SYNTH_ELF_H_ michael@0: #define COMMON_LINUX_SYNTH_ELF_H_ michael@0: michael@0: #include "common/test_assembler.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "common/using_std_string.h" michael@0: michael@0: namespace google_breakpad { michael@0: namespace synth_elf { michael@0: michael@0: using std::list; michael@0: using std::vector; michael@0: using std::map; michael@0: using std::pair; michael@0: using test_assembler::Endianness; michael@0: using test_assembler::kLittleEndian; michael@0: using test_assembler::kUnsetEndian; michael@0: using test_assembler::Label; michael@0: using test_assembler::Section; michael@0: michael@0: // String tables are common in ELF headers, so subclass Section michael@0: // to make them easy to generate. michael@0: class StringTable : public Section { michael@0: public: michael@0: StringTable(Endianness endianness = kUnsetEndian) michael@0: : Section(endianness) { michael@0: start() = 0; michael@0: empty_string = Add(""); michael@0: } michael@0: michael@0: // Add the string s to the string table, and return michael@0: // a label containing the offset into the string table michael@0: // at which it was added. michael@0: Label Add(const string& s) { michael@0: if (strings_.find(s) != strings_.end()) michael@0: return strings_[s]; michael@0: michael@0: Label string_label(Here()); michael@0: AppendCString(s); michael@0: strings_[s] = string_label; michael@0: return string_label; michael@0: } michael@0: michael@0: // All StringTables contain an empty string as their first michael@0: // entry. michael@0: Label empty_string; michael@0: michael@0: // Avoid inserting duplicate strings. michael@0: map strings_; michael@0: }; michael@0: michael@0: // A Section representing an entire ELF file. michael@0: class ELF : public Section { michael@0: public: michael@0: ELF(uint16_t machine, // EM_386, etc michael@0: uint8_t file_class, // ELFCLASS{32,64} michael@0: Endianness endianness = kLittleEndian); michael@0: michael@0: // Add the Section section to the section header table and append it michael@0: // to the file. Returns the index of the section in the section michael@0: // header table. michael@0: int AddSection(const string& name, const Section& section, michael@0: uint32_t type, uint32_t flags = 0, uint64_t addr = 0, michael@0: uint32_t link = 0, uint64_t entsize = 0, uint64_t offset = 0); michael@0: michael@0: // Add a segment containing from section index start to section index end. michael@0: // The indexes must have been gotten from AddSection. michael@0: void AddSegment(int start, int end, uint32_t type, uint32_t flags = 0); michael@0: michael@0: // Write out all data. GetContents may be used after this. michael@0: void Finish(); michael@0: michael@0: private: michael@0: // Size of an address, in bytes. michael@0: const size_t addr_size_; michael@0: michael@0: // Offset to the program header table. michael@0: Label program_header_label_; michael@0: // Number of entries in the program header table. michael@0: int program_count_; michael@0: Label program_count_label_; michael@0: // The program header table itself. michael@0: Section program_header_table_; michael@0: michael@0: // Offset to the section header table. michael@0: Label section_header_label_; michael@0: // Number of entries in the section header table. michael@0: int section_count_; michael@0: Label section_count_label_; michael@0: // The section header table itself. michael@0: Section section_header_table_; michael@0: michael@0: // Index of the section header string table in the section michael@0: // header table. michael@0: Label section_header_string_index_; michael@0: // Section containing the names of section header table entries. michael@0: StringTable section_header_strings_; michael@0: michael@0: // Record of an added section michael@0: struct ElfSection : public Section { michael@0: ElfSection(const Section& section, uint32_t type, uint32_t addr, michael@0: uint32_t offset, Label offset_label, uint32_t size) michael@0: : Section(section), type_(type), addr_(addr), offset_(offset) michael@0: , offset_label_(offset_label), size_(size) { michael@0: } michael@0: michael@0: uint32_t type_; michael@0: uint32_t addr_; michael@0: uint32_t offset_; michael@0: Label offset_label_; michael@0: uint32_t size_; michael@0: }; michael@0: michael@0: vector sections_; michael@0: michael@0: void AppendSection(ElfSection §ion); michael@0: }; michael@0: michael@0: // A class to build .symtab or .dynsym sections. michael@0: class SymbolTable : public Section { michael@0: public: michael@0: // table is the StringTable that contains symbol names. The caller michael@0: // must ensure that it remains alive for the life of the michael@0: // SymbolTable. michael@0: SymbolTable(Endianness endianness, size_t addr_size, StringTable& table); michael@0: michael@0: // Add an Elf32_Sym. michael@0: void AddSymbol(const string& name, uint32_t value, michael@0: uint32_t size, unsigned info, uint16_t shndx); michael@0: // Add an Elf64_Sym. michael@0: void AddSymbol(const string& name, uint64_t value, michael@0: uint64_t size, unsigned info, uint16_t shndx); michael@0: michael@0: private: michael@0: size_t addr_size_; michael@0: StringTable& table_; michael@0: }; michael@0: michael@0: // A class for note sections michael@0: class Notes : public Section { michael@0: public: michael@0: Notes(Endianness endianness) michael@0: : Section(endianness) { michael@0: } michael@0: michael@0: // Add a note. michael@0: void AddNote(int type, const string &name, const uint8_t* desc_bytes, michael@0: size_t desc_size); michael@0: }; michael@0: michael@0: } // namespace synth_elf michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // COMMON_LINUX_SYNTH_ELF_H_