encoded_program.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "courgette/encoded_program.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <algorithm>
  8. #include <map>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/environment.h"
  13. #include "base/logging.h"
  14. #include "base/memory/raw_ptr.h"
  15. #include "base/numerics/safe_conversions.h"
  16. #include "base/numerics/safe_math.h"
  17. #include "base/strings/string_number_conversions.h"
  18. #include "base/strings/string_util.h"
  19. #include "courgette/label_manager.h"
  20. #include "courgette/streams.h"
  21. namespace courgette {
  22. namespace {
  23. // Serializes a vector of integral values using Varint32 coding.
  24. template<typename V>
  25. CheckBool WriteVector(const V& items, SinkStream* buffer) {
  26. size_t count = items.size();
  27. bool ok = buffer->WriteSizeVarint32(count);
  28. for (size_t i = 0; ok && i < count; ++i) {
  29. ok = buffer->WriteSizeVarint32(items[i]);
  30. }
  31. return ok;
  32. }
  33. template<typename V>
  34. bool ReadVector(V* items, SourceStream* buffer) {
  35. uint32_t count;
  36. if (!buffer->ReadVarint32(&count))
  37. return false;
  38. items->clear();
  39. bool ok = items->reserve(count);
  40. for (size_t i = 0; ok && i < count; ++i) {
  41. uint32_t item;
  42. ok = buffer->ReadVarint32(&item);
  43. if (ok)
  44. ok = items->push_back(static_cast<typename V::value_type>(item));
  45. }
  46. return ok;
  47. }
  48. // Serializes a vector, using delta coding followed by Varint32Signed coding.
  49. template<typename V>
  50. CheckBool WriteSigned32Delta(const V& set, SinkStream* buffer) {
  51. size_t count = set.size();
  52. bool ok = buffer->WriteSizeVarint32(count);
  53. uint32_t prev = 0;
  54. for (size_t i = 0; ok && i < count; ++i) {
  55. uint32_t current = set[i];
  56. int32_t delta = current - prev;
  57. ok = buffer->WriteVarint32Signed(delta);
  58. prev = current;
  59. }
  60. return ok;
  61. }
  62. template <typename V>
  63. static CheckBool ReadSigned32Delta(V* set, SourceStream* buffer) {
  64. uint32_t count;
  65. if (!buffer->ReadVarint32(&count))
  66. return false;
  67. set->clear();
  68. bool ok = set->reserve(count);
  69. uint32_t prev = 0;
  70. for (size_t i = 0; ok && i < count; ++i) {
  71. int32_t delta;
  72. ok = buffer->ReadVarint32Signed(&delta);
  73. if (ok) {
  74. uint32_t current = static_cast<uint32_t>(prev + delta);
  75. ok = set->push_back(current);
  76. prev = current;
  77. }
  78. }
  79. return ok;
  80. }
  81. // Write a vector as the byte representation of the contents.
  82. //
  83. // (This only really makes sense for a type T that has sizeof(T)==1, otherwise
  84. // serialized representation is not endian-agnostic. But it is useful to keep
  85. // the possibility of a greater size for experiments comparing Varint32 encoding
  86. // of a vector of larger integrals vs a plain form.)
  87. //
  88. template<typename V>
  89. CheckBool WriteVectorU8(const V& items, SinkStream* buffer) {
  90. size_t count = items.size();
  91. bool ok = buffer->WriteSizeVarint32(count);
  92. if (count != 0 && ok) {
  93. size_t byte_count = count * sizeof(typename V::value_type);
  94. ok = buffer->Write(static_cast<const void*>(&items[0]), byte_count);
  95. }
  96. return ok;
  97. }
  98. template<typename V>
  99. bool ReadVectorU8(V* items, SourceStream* buffer) {
  100. uint32_t count;
  101. if (!buffer->ReadVarint32(&count))
  102. return false;
  103. items->clear();
  104. bool ok = items->resize(count, 0);
  105. if (ok && count != 0) {
  106. size_t byte_count = count * sizeof(typename V::value_type);
  107. return buffer->Read(static_cast<void*>(&((*items)[0])), byte_count);
  108. }
  109. return ok;
  110. }
  111. /******** InstructionStoreReceptor ********/
  112. // An InstructionReceptor that stores emitted instructions.
  113. class InstructionStoreReceptor : public InstructionReceptor {
  114. public:
  115. explicit InstructionStoreReceptor(ExecutableType exe_type,
  116. EncodedProgram* encoded)
  117. : exe_type_(exe_type), encoded_(encoded) {
  118. CHECK(encoded_);
  119. }
  120. InstructionStoreReceptor(const InstructionStoreReceptor&) = delete;
  121. InstructionStoreReceptor& operator=(const InstructionStoreReceptor&) = delete;
  122. CheckBool EmitPeRelocs() override {
  123. return encoded_->AddPeMakeRelocs(exe_type_);
  124. }
  125. CheckBool EmitElfRelocation() override {
  126. return encoded_->AddElfMakeRelocs();
  127. }
  128. CheckBool EmitOrigin(RVA rva) override { return encoded_->AddOrigin(rva); }
  129. CheckBool EmitSingleByte(uint8_t byte) override {
  130. return encoded_->AddCopy(1, &byte);
  131. }
  132. CheckBool EmitMultipleBytes(const uint8_t* bytes, size_t len) override {
  133. return encoded_->AddCopy(len, bytes);
  134. }
  135. CheckBool EmitRel32(Label* label) override {
  136. return encoded_->AddRel32(label->index_);
  137. }
  138. CheckBool EmitAbs32(Label* label) override {
  139. return encoded_->AddAbs32(label->index_);
  140. }
  141. CheckBool EmitAbs64(Label* label) override {
  142. return encoded_->AddAbs64(label->index_);
  143. }
  144. private:
  145. ExecutableType exe_type_;
  146. raw_ptr<EncodedProgram> encoded_;
  147. };
  148. } // namespace
  149. ////////////////////////////////////////////////////////////////////////////////
  150. // Constructor is here rather than in the header. Although the constructor
  151. // appears to do nothing it is fact quite large because of the implicit calls to
  152. // field constructors. Ditto for the destructor.
  153. EncodedProgram::EncodedProgram() = default;
  154. EncodedProgram::~EncodedProgram() = default;
  155. CheckBool EncodedProgram::ImportLabels(
  156. const LabelManager& abs32_label_manager,
  157. const LabelManager& rel32_label_manager) {
  158. if (!WriteRvasToList(abs32_label_manager, &abs32_rva_) ||
  159. !WriteRvasToList(rel32_label_manager, &rel32_rva_)) {
  160. return false;
  161. }
  162. FillUnassignedRvaSlots(&abs32_rva_);
  163. FillUnassignedRvaSlots(&rel32_rva_);
  164. return true;
  165. }
  166. CheckBool EncodedProgram::AddOrigin(RVA origin) {
  167. return ops_.push_back(ORIGIN) && origins_.push_back(origin);
  168. }
  169. CheckBool EncodedProgram::AddCopy(size_t count, const void* bytes) {
  170. const uint8_t* source = static_cast<const uint8_t*>(bytes);
  171. bool ok = true;
  172. // Fold adjacent COPY instructions into one. This nearly halves the size of
  173. // an EncodedProgram with only COPY1 instructions since there are approx plain
  174. // 16 bytes per reloc. This has a working-set benefit during decompression.
  175. // For compression of files with large differences this makes a small (4%)
  176. // improvement in size. For files with small differences this degrades the
  177. // compressed size by 1.3%
  178. if (!ops_.empty()) {
  179. if (ops_.back() == COPY1) {
  180. ops_.back() = COPY;
  181. ok = copy_counts_.push_back(1);
  182. }
  183. if (ok && ops_.back() == COPY) {
  184. copy_counts_.back() += count;
  185. for (size_t i = 0; ok && i < count; ++i) {
  186. ok = copy_bytes_.push_back(source[i]);
  187. }
  188. return ok;
  189. }
  190. }
  191. if (ok) {
  192. if (count == 1) {
  193. ok = ops_.push_back(COPY1) && copy_bytes_.push_back(source[0]);
  194. } else {
  195. ok = ops_.push_back(COPY) && copy_counts_.push_back(count);
  196. for (size_t i = 0; ok && i < count; ++i) {
  197. ok = copy_bytes_.push_back(source[i]);
  198. }
  199. }
  200. }
  201. return ok;
  202. }
  203. CheckBool EncodedProgram::AddAbs32(int label_index) {
  204. return ops_.push_back(ABS32) && abs32_ix_.push_back(label_index);
  205. }
  206. CheckBool EncodedProgram::AddAbs64(int label_index) {
  207. return ops_.push_back(ABS64) && abs32_ix_.push_back(label_index);
  208. }
  209. CheckBool EncodedProgram::AddRel32(int label_index) {
  210. return ops_.push_back(REL32) && rel32_ix_.push_back(label_index);
  211. }
  212. CheckBool EncodedProgram::AddPeMakeRelocs(ExecutableType kind) {
  213. if (kind == EXE_WIN_32_X86)
  214. return ops_.push_back(MAKE_PE_RELOCATION_TABLE);
  215. return ops_.push_back(MAKE_PE64_RELOCATION_TABLE);
  216. }
  217. CheckBool EncodedProgram::AddElfMakeRelocs() {
  218. return ops_.push_back(MAKE_ELF_RELOCATION_TABLE);
  219. }
  220. void EncodedProgram::DebuggingSummary() {
  221. VLOG(1) << "EncodedProgram Summary"
  222. << "\n image base " << image_base_
  223. << "\n abs32 rvas " << abs32_rva_.size()
  224. << "\n rel32 rvas " << rel32_rva_.size()
  225. << "\n ops " << ops_.size()
  226. << "\n origins " << origins_.size()
  227. << "\n copy_counts " << copy_counts_.size()
  228. << "\n copy_bytes " << copy_bytes_.size()
  229. << "\n abs32_ix " << abs32_ix_.size()
  230. << "\n rel32_ix " << rel32_ix_.size();
  231. }
  232. ////////////////////////////////////////////////////////////////////////////////
  233. // For algorithm refinement purposes it is useful to write subsets of the file
  234. // format. This gives us the ability to estimate the entropy of the
  235. // differential compression of the individual streams, which can provide
  236. // invaluable insights. The default, of course, is to include all the streams.
  237. //
  238. enum FieldSelect {
  239. INCLUDE_ABS32_ADDRESSES = 0x0001,
  240. INCLUDE_REL32_ADDRESSES = 0x0002,
  241. INCLUDE_ABS32_INDEXES = 0x0010,
  242. INCLUDE_REL32_INDEXES = 0x0020,
  243. INCLUDE_OPS = 0x0100,
  244. INCLUDE_BYTES = 0x0200,
  245. INCLUDE_COPY_COUNTS = 0x0400,
  246. INCLUDE_MISC = 0x1000
  247. };
  248. static FieldSelect GetFieldSelect() {
  249. // TODO(sra): Use better configuration.
  250. std::unique_ptr<base::Environment> env(base::Environment::Create());
  251. std::string s;
  252. env->GetVar("A_FIELDS", &s);
  253. uint64_t fields;
  254. if (!base::StringToUint64(s, &fields))
  255. return static_cast<FieldSelect>(~0);
  256. return static_cast<FieldSelect>(fields);
  257. }
  258. CheckBool EncodedProgram::WriteTo(SinkStreamSet* streams) {
  259. FieldSelect select = GetFieldSelect();
  260. // The order of fields must be consistent in WriteTo and ReadFrom, regardless
  261. // of the streams used. The code can be configured with all kStreamXXX
  262. // constants the same.
  263. //
  264. // If we change the code to pipeline reading with assembly (to avoid temporary
  265. // storage vectors by consuming operands directly from the stream) then we
  266. // need to read the base address and the random access address tables first,
  267. // the rest can be interleaved.
  268. if (select & INCLUDE_MISC) {
  269. uint32_t high = static_cast<uint32_t>(image_base_ >> 32);
  270. uint32_t low = static_cast<uint32_t>(image_base_ & 0xffffffffU);
  271. if (!streams->stream(kStreamMisc)->WriteVarint32(high) ||
  272. !streams->stream(kStreamMisc)->WriteVarint32(low)) {
  273. return false;
  274. }
  275. }
  276. bool success = true;
  277. if (select & INCLUDE_ABS32_ADDRESSES) {
  278. success &= WriteSigned32Delta(abs32_rva_,
  279. streams->stream(kStreamAbs32Addresses));
  280. }
  281. if (select & INCLUDE_REL32_ADDRESSES) {
  282. success &= WriteSigned32Delta(rel32_rva_,
  283. streams->stream(kStreamRel32Addresses));
  284. }
  285. if (select & INCLUDE_MISC)
  286. success &= WriteVector(origins_, streams->stream(kStreamOriginAddresses));
  287. if (select & INCLUDE_OPS) {
  288. // 5 for length.
  289. success &= streams->stream(kStreamOps)->Reserve(ops_.size() + 5);
  290. success &= WriteVector(ops_, streams->stream(kStreamOps));
  291. }
  292. if (select & INCLUDE_COPY_COUNTS)
  293. success &= WriteVector(copy_counts_, streams->stream(kStreamCopyCounts));
  294. if (select & INCLUDE_BYTES)
  295. success &= WriteVectorU8(copy_bytes_, streams->stream(kStreamBytes));
  296. if (select & INCLUDE_ABS32_INDEXES)
  297. success &= WriteVector(abs32_ix_, streams->stream(kStreamAbs32Indexes));
  298. if (select & INCLUDE_REL32_INDEXES)
  299. success &= WriteVector(rel32_ix_, streams->stream(kStreamRel32Indexes));
  300. return success;
  301. }
  302. bool EncodedProgram::ReadFrom(SourceStreamSet* streams) {
  303. uint32_t high;
  304. uint32_t low;
  305. if (!streams->stream(kStreamMisc)->ReadVarint32(&high) ||
  306. !streams->stream(kStreamMisc)->ReadVarint32(&low)) {
  307. return false;
  308. }
  309. image_base_ = (static_cast<uint64_t>(high) << 32) | low;
  310. if (!ReadSigned32Delta(&abs32_rva_, streams->stream(kStreamAbs32Addresses)))
  311. return false;
  312. if (!ReadSigned32Delta(&rel32_rva_, streams->stream(kStreamRel32Addresses)))
  313. return false;
  314. if (!ReadVector(&origins_, streams->stream(kStreamOriginAddresses)))
  315. return false;
  316. if (!ReadVector(&ops_, streams->stream(kStreamOps)))
  317. return false;
  318. if (!ReadVector(&copy_counts_, streams->stream(kStreamCopyCounts)))
  319. return false;
  320. if (!ReadVectorU8(&copy_bytes_, streams->stream(kStreamBytes)))
  321. return false;
  322. if (!ReadVector(&abs32_ix_, streams->stream(kStreamAbs32Indexes)))
  323. return false;
  324. if (!ReadVector(&rel32_ix_, streams->stream(kStreamRel32Indexes)))
  325. return false;
  326. // Check that streams have been completely consumed.
  327. for (int i = 0; i < kStreamLimit; ++i) {
  328. if (streams->stream(i)->Remaining() > 0)
  329. return false;
  330. }
  331. return true;
  332. }
  333. // Safe, non-throwing version of std::vector::at(). Returns 'true' for success,
  334. // 'false' for out-of-bounds index error.
  335. template<typename V, typename T>
  336. bool VectorAt(const V& v, size_t index, T* output) {
  337. if (index >= v.size())
  338. return false;
  339. *output = v[index];
  340. return true;
  341. }
  342. CheckBool EncodedProgram::AssembleTo(SinkStream* final_buffer) {
  343. // For the most part, the assembly process walks the various tables.
  344. // ix_mumble is the index into the mumble table.
  345. size_t ix_origins = 0;
  346. size_t ix_copy_counts = 0;
  347. size_t ix_copy_bytes = 0;
  348. size_t ix_abs32_ix = 0;
  349. size_t ix_rel32_ix = 0;
  350. RVA current_rva = 0;
  351. bool pending_pe_relocation_table = false;
  352. uint8_t pending_pe_relocation_table_type = 0x03; // IMAGE_REL_BASED_HIGHLOW
  353. Elf32_Word pending_elf_relocation_table_type = 0;
  354. SinkStream bytes_following_relocation_table;
  355. SinkStream* output = final_buffer;
  356. for (size_t ix_ops = 0; ix_ops < ops_.size(); ++ix_ops) {
  357. OP op = ops_[ix_ops];
  358. switch (op) {
  359. default:
  360. return false;
  361. case ORIGIN: {
  362. RVA section_rva;
  363. if (!VectorAt(origins_, ix_origins, &section_rva))
  364. return false;
  365. ++ix_origins;
  366. current_rva = section_rva;
  367. break;
  368. }
  369. case COPY: {
  370. size_t count;
  371. if (!VectorAt(copy_counts_, ix_copy_counts, &count))
  372. return false;
  373. ++ix_copy_counts;
  374. for (size_t i = 0; i < count; ++i) {
  375. uint8_t b;
  376. if (!VectorAt(copy_bytes_, ix_copy_bytes, &b))
  377. return false;
  378. ++ix_copy_bytes;
  379. if (!output->Write(&b, 1))
  380. return false;
  381. }
  382. current_rva += static_cast<RVA>(count);
  383. break;
  384. }
  385. case COPY1: {
  386. uint8_t b;
  387. if (!VectorAt(copy_bytes_, ix_copy_bytes, &b))
  388. return false;
  389. ++ix_copy_bytes;
  390. if (!output->Write(&b, 1))
  391. return false;
  392. current_rva += 1;
  393. break;
  394. }
  395. case REL32: {
  396. uint32_t index;
  397. if (!VectorAt(rel32_ix_, ix_rel32_ix, &index))
  398. return false;
  399. ++ix_rel32_ix;
  400. RVA rva;
  401. if (!VectorAt(rel32_rva_, index, &rva))
  402. return false;
  403. uint32_t offset = (rva - (current_rva + 4));
  404. if (!output->Write(&offset, 4))
  405. return false;
  406. current_rva += 4;
  407. break;
  408. }
  409. case ABS32:
  410. case ABS64: {
  411. uint32_t index;
  412. if (!VectorAt(abs32_ix_, ix_abs32_ix, &index))
  413. return false;
  414. ++ix_abs32_ix;
  415. RVA rva;
  416. if (!VectorAt(abs32_rva_, index, &rva))
  417. return false;
  418. if (op == ABS32) {
  419. base::CheckedNumeric<uint32_t> abs32 = image_base_;
  420. abs32 += rva;
  421. uint32_t safe_abs32 = abs32.ValueOrDie();
  422. if (!abs32_relocs_.push_back(current_rva) ||
  423. !output->Write(&safe_abs32, 4)) {
  424. return false;
  425. }
  426. current_rva += 4;
  427. } else {
  428. base::CheckedNumeric<uint64_t> abs64 = image_base_;
  429. abs64 += rva;
  430. uint64_t safe_abs64 = abs64.ValueOrDie();
  431. if (!abs32_relocs_.push_back(current_rva) ||
  432. !output->Write(&safe_abs64, 8)) {
  433. return false;
  434. }
  435. current_rva += 8;
  436. }
  437. break;
  438. }
  439. case MAKE_PE_RELOCATION_TABLE: {
  440. // We can see the base relocation anywhere, but we only have the
  441. // information to generate it at the very end. So we divert the bytes
  442. // we are generating to a temporary stream.
  443. if (pending_pe_relocation_table)
  444. return false; // Can't have two base relocation tables.
  445. pending_pe_relocation_table = true;
  446. output = &bytes_following_relocation_table;
  447. break;
  448. // There is a potential problem *if* the instruction stream contains
  449. // some REL32 relocations following the base relocation and in the same
  450. // section. We don't know the size of the table, so 'current_rva' will
  451. // be wrong, causing REL32 offsets to be miscalculated. This never
  452. // happens; the base relocation table is usually in a section of its
  453. // own, a data-only section, and following everything else in the
  454. // executable except some padding zero bytes. We could fix this by
  455. // emitting an ORIGIN after the MAKE_BASE_RELOCATION_TABLE.
  456. }
  457. case MAKE_PE64_RELOCATION_TABLE: {
  458. if (pending_pe_relocation_table)
  459. return false; // Can't have two base relocation tables.
  460. pending_pe_relocation_table = true;
  461. pending_pe_relocation_table_type = 0x0A; // IMAGE_REL_BASED_DIR64
  462. output = &bytes_following_relocation_table;
  463. break;
  464. }
  465. case MAKE_ELF_RELOCATION_TABLE: {
  466. // We can see the base relocation anywhere, but we only have the
  467. // information to generate it at the very end. So we divert the bytes
  468. // we are generating to a temporary stream.
  469. if (pending_elf_relocation_table_type)
  470. return false; // Can't have two base relocation tables.
  471. pending_elf_relocation_table_type = R_386_RELATIVE;
  472. output = &bytes_following_relocation_table;
  473. break;
  474. }
  475. }
  476. }
  477. if (pending_pe_relocation_table) {
  478. if (!GeneratePeRelocations(final_buffer,
  479. pending_pe_relocation_table_type) ||
  480. !final_buffer->Append(&bytes_following_relocation_table))
  481. return false;
  482. }
  483. if (pending_elf_relocation_table_type) {
  484. if (!GenerateElfRelocations(pending_elf_relocation_table_type,
  485. final_buffer) ||
  486. !final_buffer->Append(&bytes_following_relocation_table))
  487. return false;
  488. }
  489. // Final verification check: did we consume all lists?
  490. if (ix_copy_counts != copy_counts_.size())
  491. return false;
  492. if (ix_copy_bytes != copy_bytes_.size())
  493. return false;
  494. if (ix_abs32_ix != abs32_ix_.size())
  495. return false;
  496. if (ix_rel32_ix != rel32_ix_.size())
  497. return false;
  498. return true;
  499. }
  500. CheckBool EncodedProgram::GenerateInstructions(
  501. ExecutableType exe_type,
  502. const InstructionGenerator& gen) {
  503. InstructionStoreReceptor store_receptor(exe_type, this);
  504. return gen.Run(&store_receptor);
  505. }
  506. // RelocBlock has the layout of a block of relocations in the base relocation
  507. // table file format.
  508. struct RelocBlockPOD {
  509. uint32_t page_rva;
  510. uint32_t block_size;
  511. uint16_t relocs[4096]; // Allow up to one relocation per byte of a 4k page.
  512. };
  513. static_assert(offsetof(RelocBlockPOD, relocs) == 8, "reloc block header size");
  514. class RelocBlock {
  515. public:
  516. RelocBlock() {
  517. pod.page_rva = 0xFFFFFFFF;
  518. pod.block_size = 8;
  519. }
  520. void Add(uint16_t item) {
  521. pod.relocs[(pod.block_size-8)/2] = item;
  522. pod.block_size += 2;
  523. }
  524. [[nodiscard]] CheckBool Flush(SinkStream* buffer) {
  525. bool ok = true;
  526. if (pod.block_size != 8) {
  527. if (pod.block_size % 4 != 0) { // Pad to make size multiple of 4 bytes.
  528. Add(0);
  529. }
  530. ok = buffer->Write(&pod, pod.block_size);
  531. pod.block_size = 8;
  532. }
  533. return ok;
  534. }
  535. RelocBlockPOD pod;
  536. };
  537. // static
  538. // Updates |rvas| so |rvas[label.index_] == label.rva_| for each |label| in
  539. // |label_manager|, assuming |label.index_| is properly assigned. Takes care of
  540. // |rvas| resizing. Unused slots in |rvas| are assigned |kUnassignedRVA|.
  541. // Returns true on success, and false otherwise.
  542. CheckBool EncodedProgram::WriteRvasToList(const LabelManager& label_manager,
  543. RvaVector* rvas) {
  544. rvas->clear();
  545. int index_bound = LabelManager::GetLabelIndexBound(label_manager.Labels());
  546. if (!rvas->resize(index_bound, kUnassignedRVA))
  547. return false;
  548. // For each Label, write its RVA to assigned index.
  549. for (const Label& label : label_manager.Labels()) {
  550. DCHECK_NE(label.index_, Label::kNoIndex);
  551. DCHECK_EQ((*rvas)[label.index_], kUnassignedRVA)
  552. << "ExportToList() double assigned " << label.index_;
  553. (*rvas)[label.index_] = label.rva_;
  554. }
  555. return true;
  556. }
  557. // static
  558. // Replaces all unassigned slots in |rvas| with the value at the previous index
  559. // so they delta-encode to zero. (There might be better values than zero. The
  560. // way to get that is have the higher level assembly program assign the
  561. // unassigned slots.)
  562. void EncodedProgram::FillUnassignedRvaSlots(RvaVector* rvas) {
  563. RVA previous = 0;
  564. for (RVA& rva : *rvas) {
  565. if (rva == kUnassignedRVA)
  566. rva = previous;
  567. else
  568. previous = rva;
  569. }
  570. }
  571. CheckBool EncodedProgram::GeneratePeRelocations(SinkStream* buffer,
  572. uint8_t type) {
  573. std::sort(abs32_relocs_.begin(), abs32_relocs_.end());
  574. DCHECK(abs32_relocs_.empty() || abs32_relocs_.back() != kUnassignedRVA);
  575. RelocBlock block;
  576. bool ok = true;
  577. for (size_t i = 0; ok && i < abs32_relocs_.size(); ++i) {
  578. uint32_t rva = abs32_relocs_[i];
  579. uint32_t page_rva = rva & ~0xFFF;
  580. if (page_rva != block.pod.page_rva) {
  581. ok &= block.Flush(buffer);
  582. block.pod.page_rva = page_rva;
  583. }
  584. if (ok)
  585. block.Add(((static_cast<uint16_t>(type)) << 12) | (rva & 0xFFF));
  586. }
  587. ok &= block.Flush(buffer);
  588. return ok;
  589. }
  590. CheckBool EncodedProgram::GenerateElfRelocations(Elf32_Word r_info,
  591. SinkStream* buffer) {
  592. std::sort(abs32_relocs_.begin(), abs32_relocs_.end());
  593. DCHECK(abs32_relocs_.empty() || abs32_relocs_.back() != kUnassignedRVA);
  594. Elf32_Rel relocation_block;
  595. relocation_block.r_info = r_info;
  596. bool ok = true;
  597. for (size_t i = 0; ok && i < abs32_relocs_.size(); ++i) {
  598. relocation_block.r_offset = abs32_relocs_[i];
  599. ok = buffer->Write(&relocation_block, sizeof(Elf32_Rel));
  600. }
  601. return ok;
  602. }
  603. ////////////////////////////////////////////////////////////////////////////////
  604. Status WriteEncodedProgram(EncodedProgram* encoded, SinkStreamSet* sink) {
  605. if (!encoded->WriteTo(sink))
  606. return C_STREAM_ERROR;
  607. return C_OK;
  608. }
  609. Status ReadEncodedProgram(SourceStreamSet* streams,
  610. std::unique_ptr<EncodedProgram>* output) {
  611. output->reset();
  612. std::unique_ptr<EncodedProgram> encoded(new EncodedProgram());
  613. if (!encoded->ReadFrom(streams))
  614. return C_DESERIALIZATION_FAILED;
  615. *output = std::move(encoded);
  616. return C_OK;
  617. }
  618. Status Assemble(EncodedProgram* encoded, SinkStream* buffer) {
  619. bool assembled = encoded->AssembleTo(buffer);
  620. if (assembled)
  621. return C_OK;
  622. return C_ASSEMBLY_FAILED;
  623. }
  624. } // namespace courgette