diff options
author | dgarrett@chromium.org <dgarrett@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-07 22:49:15 +0000 |
---|---|---|
committer | dgarrett@chromium.org <dgarrett@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-07 22:49:15 +0000 |
commit | 88da5078df0e51dc1c56dac1aa92608a53a03803 (patch) | |
tree | 93c6b467379da5dd3ef243e3f0e3bc353b4f3333 /courgette/encoded_program.cc | |
parent | 2ca59863a57ed182b9b5a3c89b6212c10e94ec06 (diff) | |
download | chromium_src-88da5078df0e51dc1c56dac1aa92608a53a03803.zip chromium_src-88da5078df0e51dc1c56dac1aa92608a53a03803.tar.gz chromium_src-88da5078df0e51dc1c56dac1aa92608a53a03803.tar.bz2 |
Add Elf 32 Support to Courgette.
This change takes advantage of recent refactoring and adds support for
Elf X86 32 executables to courgette. It should have no effect on handling
of Windows PE executables.
We have planned ahead to be able to restrict the code size of the courgette
library in different cases to reduce patcher sizes, but this change does
not yet take advantage of that (all platforms are supported everywhere).
Also, the patcher class currently contains a very small amount of Elf/PE
specific code for recreating relocation tables that cannot (currently) be
compiled out.
BUG=chromium-os:22149
TEST=Please verify that Chrome/Chromium patches can still be generated and
work.
Also, please see how much the updater executable which is downloaded to
users has changed in size since R16.
Review URL: http://codereview.chromium.org/8428009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@108929 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'courgette/encoded_program.cc')
-rw-r--r-- | courgette/encoded_program.cc | 67 |
1 files changed, 55 insertions, 12 deletions
diff --git a/courgette/encoded_program.cc b/courgette/encoded_program.cc index a675dc2..b5e7310 100644 --- a/courgette/encoded_program.cc +++ b/courgette/encoded_program.cc @@ -16,6 +16,7 @@ #include "base/utf_string_conversions.h" #include "courgette/courgette.h" #include "courgette/streams.h" +#include "courgette/types_elf.h" namespace courgette { @@ -241,8 +242,12 @@ CheckBool EncodedProgram::AddRel32(int label_index) { return ops_.push_back(REL32) && rel32_ix_.push_back(label_index); } -CheckBool EncodedProgram::AddMakeRelocs() { - return ops_.push_back(MAKE_BASE_RELOCATION_TABLE); +CheckBool EncodedProgram::AddPeMakeRelocs() { + return ops_.push_back(MAKE_PE_RELOCATION_TABLE); +} + +CheckBool EncodedProgram::AddElfMakeRelocs() { + return ops_.push_back(MAKE_ELF_RELOCATION_TABLE); } void EncodedProgram::DebuggingSummary() { @@ -399,8 +404,9 @@ CheckBool EncodedProgram::AssembleTo(SinkStream* final_buffer) { RVA current_rva = 0; - bool pending_base_relocation_table = false; - SinkStream bytes_following_base_relocation_table; + bool pending_pe_relocation_table = false; + bool pending_elf_relocation_table = false; + SinkStream bytes_following_relocation_table; SinkStream* output = final_buffer; @@ -478,16 +484,16 @@ CheckBool EncodedProgram::AssembleTo(SinkStream* final_buffer) { break; } - case MAKE_BASE_RELOCATION_TABLE: { + case MAKE_PE_RELOCATION_TABLE: { // We can see the base relocation anywhere, but we only have the // information to generate it at the very end. So we divert the bytes // we are generating to a temporary stream. - if (pending_base_relocation_table) // Can't have two base relocation + if (pending_pe_relocation_table) // Can't have two base relocation // tables. return false; - pending_base_relocation_table = true; - output = &bytes_following_base_relocation_table; + pending_pe_relocation_table = true; + output = &bytes_following_relocation_table; break; // There is a potential problem *if* the instruction stream contains // some REL32 relocations following the base relocation and in the same @@ -498,12 +504,31 @@ CheckBool EncodedProgram::AssembleTo(SinkStream* final_buffer) { // executable except some padding zero bytes. We could fix this by // emitting an ORIGIN after the MAKE_BASE_RELOCATION_TABLE. } + + case MAKE_ELF_RELOCATION_TABLE: { + // We can see the base relocation anywhere, but we only have the + // information to generate it at the very end. So we divert the bytes + // we are generating to a temporary stream. + if (pending_elf_relocation_table) // Can't have two relocation + // tables. + return false; + + pending_elf_relocation_table = true; + output = &bytes_following_relocation_table; + break; + } } } - if (pending_base_relocation_table) { - if (!GenerateBaseRelocations(final_buffer) || - !final_buffer->Append(&bytes_following_base_relocation_table)) + if (pending_pe_relocation_table) { + if (!GeneratePeRelocations(final_buffer) || + !final_buffer->Append(&bytes_following_relocation_table)) + return false; + } + + if (pending_elf_relocation_table) { + if (!GenerateElfRelocations(final_buffer) || + !final_buffer->Append(&bytes_following_relocation_table)) return false; } @@ -557,7 +582,7 @@ class RelocBlock { RelocBlockPOD pod; }; -CheckBool EncodedProgram::GenerateBaseRelocations(SinkStream* buffer) { +CheckBool EncodedProgram::GeneratePeRelocations(SinkStream* buffer) { std::sort(abs32_relocs_.begin(), abs32_relocs_.end()); RelocBlock block; @@ -577,6 +602,24 @@ CheckBool EncodedProgram::GenerateBaseRelocations(SinkStream* buffer) { return ok; } +CheckBool EncodedProgram::GenerateElfRelocations(SinkStream* buffer) { + std::sort(abs32_relocs_.begin(), abs32_relocs_.end()); + + Elf32_Rel relocation_block; + + // We only handle this specific type of relocation, so far. + relocation_block.r_info = R_386_RELATIVE; + + bool ok = true; + for (size_t i = 0; ok && i < abs32_relocs_.size(); ++i) { + relocation_block.r_offset = abs32_relocs_[i]; + ok = buffer->Write(&relocation_block, sizeof(Elf32_Rel)); + } + + printf("Emitting size %ld\n", sizeof(Elf32_Rel) * abs32_relocs_.size()); + + return ok; +} //////////////////////////////////////////////////////////////////////////////// Status WriteEncodedProgram(EncodedProgram* encoded, SinkStreamSet* sink) { |