123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- // Copyright 2013 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #ifndef COURGETTE_DISASSEMBLER_ELF_32_H_
- #define COURGETTE_DISASSEMBLER_ELF_32_H_
- #include <stddef.h>
- #include <stdint.h>
- #include <memory>
- #include <string>
- #include <vector>
- #include "base/memory/raw_ptr.h"
- #include "courgette/disassembler.h"
- #include "courgette/image_utils.h"
- #include "courgette/instruction_utils.h"
- #include "courgette/memory_allocator.h"
- #include "courgette/types_elf.h"
- namespace courgette {
- class AssemblyProgram;
- // A Courgette disassembler for 32-bit ELF files. This is only a partial
- // implementation that admits subclasses for the architecture-specific parts of
- // 32-bit ELF file processing. Specifically:
- // - RelToRVA() processes entries in ELF relocation table.
- // - ParseRelocationSection() verifies the organization of the ELF relocation
- // table.
- // - ParseRel32RelocsFromSection() finds branch targets by looking for relative
- // branch/call opcodes in the particular architecture's machine code.
- class DisassemblerElf32 : public Disassembler {
- public:
- // Different instructions encode the target rva differently. This
- // class encapsulates this behavior. public for use in unit tests.
- class TypedRVA {
- public:
- explicit TypedRVA(RVA rva) : rva_(rva) { }
- virtual ~TypedRVA() { }
- RVA rva() const { return rva_; }
- RVA relative_target() const { return relative_target_; }
- FileOffset file_offset() const { return file_offset_; }
- void set_relative_target(RVA relative_target) {
- relative_target_ = relative_target;
- }
- void set_file_offset(FileOffset file_offset) { file_offset_ = file_offset; }
- // Computes the relative jump's offset from the op in p.
- virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0;
- // Emits the assembly instruction corresponding to |label|.
- virtual CheckBool EmitInstruction(Label* label,
- InstructionReceptor* receptor) = 0;
- // Returns the size of the instruction containing the RVA.
- virtual uint16_t op_size() const = 0;
- // Comparator for sorting, which assumes uniqueness of RVAs.
- static bool IsLessThanByRVA(const std::unique_ptr<TypedRVA>& a,
- const std::unique_ptr<TypedRVA>& b) {
- return a->rva() < b->rva();
- }
- // Comparator for sorting, which assumes uniqueness of file offsets.
- static bool IsLessThanByFileOffset(const std::unique_ptr<TypedRVA>& a,
- const std::unique_ptr<TypedRVA>& b) {
- return a->file_offset() < b->file_offset();
- }
- private:
- const RVA rva_;
- RVA relative_target_ = kNoRVA;
- FileOffset file_offset_ = kNoFileOffset;
- };
- // Visitor/adaptor to translate RVA to target RVA. This is the ELF
- // counterpart to RvaVisitor_Rel32 that uses TypedRVA.
- class Elf32RvaVisitor_Rel32 :
- public VectorRvaVisitor<std::unique_ptr<TypedRVA>> {
- public:
- Elf32RvaVisitor_Rel32(
- const std::vector<std::unique_ptr<TypedRVA>>& rva_locations);
- Elf32RvaVisitor_Rel32(const Elf32RvaVisitor_Rel32&) = delete;
- Elf32RvaVisitor_Rel32& operator=(const Elf32RvaVisitor_Rel32&) = delete;
- ~Elf32RvaVisitor_Rel32() override { }
- // VectorRvaVisitor<TypedRVA*> interfaces.
- RVA Get() const override;
- };
- public:
- DisassemblerElf32(const uint8_t* start, size_t length);
- DisassemblerElf32(const DisassemblerElf32&) = delete;
- DisassemblerElf32& operator=(const DisassemblerElf32&) = delete;
- ~DisassemblerElf32() override { }
- // Disassembler interfaces.
- RVA FileOffsetToRVA(FileOffset file_offset) const override;
- FileOffset RVAToFileOffset(RVA rva) const override;
- RVA PointerToTargetRVA(const uint8_t* p) const override;
- ExecutableType kind() const override = 0;
- uint64_t image_base() const override { return 0; }
- bool ParseHeader() override;
- virtual e_machine_values ElfEM() const = 0;
- [[nodiscard]] CheckBool IsValidTargetRVA(RVA rva) const;
- // Converts an ELF relocation instruction into an RVA.
- [[nodiscard]] virtual CheckBool RelToRVA(Elf32_Rel rel,
- RVA* result) const = 0;
- // Public for unittests only
- std::vector<RVA>& Abs32Locations() { return abs32_locations_; }
- std::vector<std::unique_ptr<TypedRVA>>& Rel32Locations() {
- return rel32_locations_;
- }
- protected:
- // Returns 'true' if an valid executable is detected using only quick checks.
- // Derived classes should inject |elf_em| corresponding to their architecture,
- // which will be checked against the detected one.
- static bool QuickDetect(const uint8_t* start,
- size_t length,
- e_machine_values elf_em);
- // Returns whether all non-SHT_NOBITS sections lie within image.
- bool CheckSectionRanges();
- // Returns whether all program segments lie within image.
- bool CheckProgramSegmentRanges();
- void UpdateLength();
- // Misc Section Helpers
- Elf32_Half SectionHeaderCount() const {
- return section_header_table_size_;
- }
- const Elf32_Shdr* SectionHeader(Elf32_Half id) const {
- assert(id >= 0 && id < SectionHeaderCount());
- return §ion_header_table_[id];
- }
- const uint8_t* SectionBody(Elf32_Half id) const {
- const Elf32_Shdr* section_header = SectionHeader(id);
- DCHECK(section_header->sh_type != SHT_NOBITS);
- return FileOffsetToPointer(section_header->sh_offset);
- }
- // Gets the |name| of section |shdr|. Returns true on success.
- CheckBool SectionName(const Elf32_Shdr& shdr, std::string* name) const;
- // Misc Segment Helpers
- Elf32_Half ProgramSegmentHeaderCount() const {
- return program_header_table_size_;
- }
- const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const {
- assert(id >= 0 && id < ProgramSegmentHeaderCount());
- return program_header_table_ + id;
- }
- // Misc address space helpers
- CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas,
- std::vector<FileOffset>* file_offsets) const;
- CheckBool RVAsToFileOffsets(
- std::vector<std::unique_ptr<TypedRVA>>* typed_rvas) const;
- // Helpers for ParseFile().
- [[nodiscard]] virtual CheckBool ParseRelocationSection(
- const Elf32_Shdr* section_header,
- InstructionReceptor* receptor) const = 0;
- [[nodiscard]] virtual CheckBool ParseRel32RelocsFromSection(
- const Elf32_Shdr* section) = 0;
- [[nodiscard]] CheckBool ParseAbs32Relocs();
- // Extracts all rel32 TypedRVAs. Does not sort the result.
- [[nodiscard]] CheckBool ParseRel32RelocsFromSections();
- // Disassembler interfaces.
- bool ExtractAbs32Locations() override;
- bool ExtractRel32Locations() override;
- RvaVisitor* CreateAbs32TargetRvaVisitor() override;
- RvaVisitor* CreateRel32TargetRvaVisitor() override;
- void RemoveUnusedRel32Locations(AssemblyProgram* program) override;
- InstructionGenerator GetInstructionGenerator(
- AssemblyProgram* program) override;
- [[nodiscard]] CheckBool ParseFile(AssemblyProgram* target,
- InstructionReceptor* receptor) const;
- [[nodiscard]] CheckBool ParseProgbitsSection(
- const Elf32_Shdr* section_header,
- std::vector<FileOffset>::iterator* current_abs_offset,
- std::vector<FileOffset>::iterator end_abs_offset,
- std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel,
- std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel,
- AssemblyProgram* program,
- InstructionReceptor* receptor) const;
- [[nodiscard]] CheckBool ParseSimpleRegion(
- FileOffset start_file_offset,
- FileOffset end_file_offset,
- InstructionReceptor* receptor) const;
- [[nodiscard]] CheckBool CheckSection(RVA rva);
- raw_ptr<const Elf32_Ehdr> header_;
- Elf32_Half section_header_table_size_;
- // Section header table, ordered by section id.
- std::vector<Elf32_Shdr> section_header_table_;
- // An ordering of |section_header_table_|, sorted by file offset.
- std::vector<Elf32_Half> section_header_file_offset_order_;
- raw_ptr<const Elf32_Phdr> program_header_table_;
- Elf32_Half program_header_table_size_;
- // Pointer to string table containing section names.
- const char* default_string_section_;
- size_t default_string_section_size_;
- // Sorted abs32 RVAs.
- std::vector<RVA> abs32_locations_;
- // Sorted rel32 RVAs. This is mutable because ParseFile() temporarily sorts
- // these by file offsets.
- mutable std::vector<std::unique_ptr<TypedRVA>> rel32_locations_;
- };
- } // namespace courgette
- #endif // COURGETTE_DISASSEMBLER_ELF_32_H_
|