OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "courgette/assembly_program.h" | 5 #include "courgette/assembly_program.h" |
6 | 6 |
7 #include <memory.h> | 7 #include <memory.h> |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
8 #include <algorithm> | 10 #include <algorithm> |
9 #include <map> | 11 #include <map> |
10 #include <set> | 12 #include <set> |
11 #include <sstream> | 13 #include <sstream> |
12 #include <vector> | 14 #include <vector> |
13 | 15 |
14 #include "base/logging.h" | 16 #include "base/logging.h" |
15 #include "base/macros.h" | 17 #include "base/macros.h" |
16 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
17 | 19 |
(...skipping 27 matching lines...) Expand all Loading... |
45 | 47 |
46 // Emits an ELF ARM relocation table. | 48 // Emits an ELF ARM relocation table. |
47 class ElfARMRelocsInstruction : public Instruction { | 49 class ElfARMRelocsInstruction : public Instruction { |
48 public: | 50 public: |
49 ElfARMRelocsInstruction() : Instruction(MAKEELFARMRELOCS) {} | 51 ElfARMRelocsInstruction() : Instruction(MAKEELFARMRELOCS) {} |
50 }; | 52 }; |
51 | 53 |
52 // Emits a single byte. | 54 // Emits a single byte. |
53 class ByteInstruction : public Instruction { | 55 class ByteInstruction : public Instruction { |
54 public: | 56 public: |
55 explicit ByteInstruction(uint8 value) : Instruction(DEFBYTE, value) {} | 57 explicit ByteInstruction(uint8_t value) : Instruction(DEFBYTE, value) {} |
56 uint8 byte_value() const { return info_; } | 58 uint8_t byte_value() const { return info_; } |
57 }; | 59 }; |
58 | 60 |
59 // Emits a single byte. | 61 // Emits a single byte. |
60 class BytesInstruction : public Instruction { | 62 class BytesInstruction : public Instruction { |
61 public: | 63 public: |
62 BytesInstruction(const uint8* values, size_t len) | 64 BytesInstruction(const uint8_t* values, size_t len) |
63 : Instruction(DEFBYTES, 0), | 65 : Instruction(DEFBYTES, 0), values_(values), len_(len) {} |
64 values_(values), | 66 const uint8_t* byte_values() const { return values_; } |
65 len_(len) {} | |
66 const uint8* byte_values() const { return values_; } | |
67 size_t len() const { return len_; } | 67 size_t len() const { return len_; } |
68 | 68 |
69 private: | 69 private: |
70 const uint8* values_; | 70 const uint8_t* values_; |
71 size_t len_; | 71 size_t len_; |
72 }; | 72 }; |
73 | 73 |
74 // A ABS32 to REL32 instruction emits a reference to a label's address. | 74 // A ABS32 to REL32 instruction emits a reference to a label's address. |
75 class InstructionWithLabel : public Instruction { | 75 class InstructionWithLabel : public Instruction { |
76 public: | 76 public: |
77 InstructionWithLabel(OP op, Label* label) | 77 InstructionWithLabel(OP op, Label* label) |
78 : Instruction(op, 0), label_(label) { | 78 : Instruction(op, 0), label_(label) { |
79 if (label == NULL) NOTREACHED(); | 79 if (label == NULL) NOTREACHED(); |
80 } | 80 } |
81 Label* label() const { return label_; } | 81 Label* label() const { return label_; } |
82 protected: | 82 protected: |
83 Label* label_; | 83 Label* label_; |
84 }; | 84 }; |
85 | 85 |
86 // An ARM REL32 instruction emits a reference to a label's address and | 86 // An ARM REL32 instruction emits a reference to a label's address and |
87 // a specially-compressed ARM op. | 87 // a specially-compressed ARM op. |
88 class InstructionWithLabelARM : public InstructionWithLabel { | 88 class InstructionWithLabelARM : public InstructionWithLabel { |
89 public: | 89 public: |
90 InstructionWithLabelARM(OP op, uint16 compressed_op, Label* label, | 90 InstructionWithLabelARM(OP op, |
91 const uint8* arm_op, uint16 op_size) | 91 uint16_t compressed_op, |
92 : InstructionWithLabel(op, label), compressed_op_(compressed_op), | 92 Label* label, |
93 arm_op_(arm_op), op_size_(op_size) { | 93 const uint8_t* arm_op, |
| 94 uint16_t op_size) |
| 95 : InstructionWithLabel(op, label), |
| 96 compressed_op_(compressed_op), |
| 97 arm_op_(arm_op), |
| 98 op_size_(op_size) { |
94 if (label == NULL) NOTREACHED(); | 99 if (label == NULL) NOTREACHED(); |
95 } | 100 } |
96 uint16 compressed_op() const { return compressed_op_; } | 101 uint16_t compressed_op() const { return compressed_op_; } |
97 const uint8* arm_op() const { return arm_op_; } | 102 const uint8_t* arm_op() const { return arm_op_; } |
98 uint16 op_size() const { return op_size_; } | 103 uint16_t op_size() const { return op_size_; } |
| 104 |
99 private: | 105 private: |
100 uint16 compressed_op_; | 106 uint16_t compressed_op_; |
101 const uint8* arm_op_; | 107 const uint8_t* arm_op_; |
102 uint16 op_size_; | 108 uint16_t op_size_; |
103 }; | 109 }; |
104 | 110 |
105 } // namespace | 111 } // namespace |
106 | 112 |
107 AssemblyProgram::AssemblyProgram(ExecutableType kind) | 113 AssemblyProgram::AssemblyProgram(ExecutableType kind) |
108 : kind_(kind), image_base_(0) { | 114 : kind_(kind), image_base_(0) { |
109 } | 115 } |
110 | 116 |
111 static void DeleteContainedLabels(const RVAToLabel& labels) { | 117 static void DeleteContainedLabels(const RVAToLabel& labels) { |
112 for (RVAToLabel::const_iterator p = labels.begin(); p != labels.end(); ++p) | 118 for (RVAToLabel::const_iterator p = labels.begin(); p != labels.end(); ++p) |
(...skipping 23 matching lines...) Expand all Loading... |
136 } | 142 } |
137 | 143 |
138 CheckBool AssemblyProgram::EmitElfARMRelocationInstruction() { | 144 CheckBool AssemblyProgram::EmitElfARMRelocationInstruction() { |
139 return Emit(ScopedInstruction(UncheckedNew<ElfARMRelocsInstruction>())); | 145 return Emit(ScopedInstruction(UncheckedNew<ElfARMRelocsInstruction>())); |
140 } | 146 } |
141 | 147 |
142 CheckBool AssemblyProgram::EmitOriginInstruction(RVA rva) { | 148 CheckBool AssemblyProgram::EmitOriginInstruction(RVA rva) { |
143 return Emit(ScopedInstruction(UncheckedNew<OriginInstruction>(rva))); | 149 return Emit(ScopedInstruction(UncheckedNew<OriginInstruction>(rva))); |
144 } | 150 } |
145 | 151 |
146 CheckBool AssemblyProgram::EmitByteInstruction(uint8 byte) { | 152 CheckBool AssemblyProgram::EmitByteInstruction(uint8_t byte) { |
147 return EmitShared(GetByteInstruction(byte)); | 153 return EmitShared(GetByteInstruction(byte)); |
148 } | 154 } |
149 | 155 |
150 CheckBool AssemblyProgram::EmitBytesInstruction(const uint8* values, | 156 CheckBool AssemblyProgram::EmitBytesInstruction(const uint8_t* values, |
151 size_t len) { | 157 size_t len) { |
152 return Emit(ScopedInstruction(UncheckedNew<BytesInstruction>(values, len))); | 158 return Emit(ScopedInstruction(UncheckedNew<BytesInstruction>(values, len))); |
153 } | 159 } |
154 | 160 |
155 CheckBool AssemblyProgram::EmitRel32(Label* label) { | 161 CheckBool AssemblyProgram::EmitRel32(Label* label) { |
156 return Emit( | 162 return Emit( |
157 ScopedInstruction(UncheckedNew<InstructionWithLabel>(REL32, label))); | 163 ScopedInstruction(UncheckedNew<InstructionWithLabel>(REL32, label))); |
158 } | 164 } |
159 | 165 |
160 CheckBool AssemblyProgram::EmitRel32ARM(uint16 op, Label* label, | 166 CheckBool AssemblyProgram::EmitRel32ARM(uint16_t op, |
161 const uint8* arm_op, uint16 op_size) { | 167 Label* label, |
| 168 const uint8_t* arm_op, |
| 169 uint16_t op_size) { |
162 return Emit(ScopedInstruction(UncheckedNew<InstructionWithLabelARM>( | 170 return Emit(ScopedInstruction(UncheckedNew<InstructionWithLabelARM>( |
163 REL32ARM, op, label, arm_op, op_size))); | 171 REL32ARM, op, label, arm_op, op_size))); |
164 } | 172 } |
165 | 173 |
166 CheckBool AssemblyProgram::EmitAbs32(Label* label) { | 174 CheckBool AssemblyProgram::EmitAbs32(Label* label) { |
167 return Emit( | 175 return Emit( |
168 ScopedInstruction(UncheckedNew<InstructionWithLabel>(ABS32, label))); | 176 ScopedInstruction(UncheckedNew<InstructionWithLabel>(ABS32, label))); |
169 } | 177 } |
170 | 178 |
171 CheckBool AssemblyProgram::EmitAbs64(Label* label) { | 179 CheckBool AssemblyProgram::EmitAbs64(Label* label) { |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 prev = 0; | 325 prev = 0; |
318 for (RVAToLabel::reverse_iterator p = labels->rbegin(); | 326 for (RVAToLabel::reverse_iterator p = labels->rbegin(); |
319 p != labels->rend(); | 327 p != labels->rend(); |
320 ++p) { | 328 ++p) { |
321 Label* current = p->second; | 329 Label* current = p->second; |
322 if (current->index_ == Label::kNoIndex) { | 330 if (current->index_ == Label::kNoIndex) { |
323 int prev_index; | 331 int prev_index; |
324 if (prev) | 332 if (prev) |
325 prev_index = prev->index_; | 333 prev_index = prev->index_; |
326 else | 334 else |
327 prev_index = static_cast<uint32>(available.size()); | 335 prev_index = static_cast<uint32_t>(available.size()); |
328 if (prev_index != 0 && | 336 if (prev_index != 0 && |
329 prev_index != Label::kNoIndex && | 337 prev_index != Label::kNoIndex && |
330 available.at(prev_index - 1)) { | 338 available.at(prev_index - 1)) { |
331 current->index_ = prev_index - 1; | 339 current->index_ = prev_index - 1; |
332 available.at(current->index_) = false; | 340 available.at(current->index_) = false; |
333 ++fill_backward_count; | 341 ++fill_backward_count; |
334 } | 342 } |
335 } | 343 } |
336 prev = current; | 344 prev = current; |
337 } | 345 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 Instruction* instruction = instructions_[i]; | 401 Instruction* instruction = instructions_[i]; |
394 | 402 |
395 switch (instruction->op()) { | 403 switch (instruction->op()) { |
396 case ORIGIN: { | 404 case ORIGIN: { |
397 OriginInstruction* org = static_cast<OriginInstruction*>(instruction); | 405 OriginInstruction* org = static_cast<OriginInstruction*>(instruction); |
398 if (!encoded->AddOrigin(org->origin_rva())) | 406 if (!encoded->AddOrigin(org->origin_rva())) |
399 return NULL; | 407 return NULL; |
400 break; | 408 break; |
401 } | 409 } |
402 case DEFBYTE: { | 410 case DEFBYTE: { |
403 uint8 b = static_cast<ByteInstruction*>(instruction)->byte_value(); | 411 uint8_t b = static_cast<ByteInstruction*>(instruction)->byte_value(); |
404 if (!encoded->AddCopy(1, &b)) | 412 if (!encoded->AddCopy(1, &b)) |
405 return NULL; | 413 return NULL; |
406 break; | 414 break; |
407 } | 415 } |
408 case DEFBYTES: { | 416 case DEFBYTES: { |
409 const uint8* byte_values = | 417 const uint8_t* byte_values = |
410 static_cast<BytesInstruction*>(instruction)->byte_values(); | 418 static_cast<BytesInstruction*>(instruction)->byte_values(); |
411 size_t len = static_cast<BytesInstruction*>(instruction)->len(); | 419 size_t len = static_cast<BytesInstruction*>(instruction)->len(); |
412 | 420 |
413 if (!encoded->AddCopy(len, byte_values)) | 421 if (!encoded->AddCopy(len, byte_values)) |
414 return NULL; | 422 return NULL; |
415 break; | 423 break; |
416 } | 424 } |
417 case REL32: { | 425 case REL32: { |
418 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); | 426 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); |
419 if (!encoded->AddRel32(label->index_)) | 427 if (!encoded->AddRel32(label->index_)) |
420 return NULL; | 428 return NULL; |
421 break; | 429 break; |
422 } | 430 } |
423 case REL32ARM: { | 431 case REL32ARM: { |
424 Label* label = | 432 Label* label = |
425 static_cast<InstructionWithLabelARM*>(instruction)->label(); | 433 static_cast<InstructionWithLabelARM*>(instruction)->label(); |
426 uint16 compressed_op = | 434 uint16_t compressed_op = |
427 static_cast<InstructionWithLabelARM*>(instruction)-> | 435 static_cast<InstructionWithLabelARM*>(instruction)->compressed_op(); |
428 compressed_op(); | |
429 if (!encoded->AddRel32ARM(compressed_op, label->index_)) | 436 if (!encoded->AddRel32ARM(compressed_op, label->index_)) |
430 return NULL; | 437 return NULL; |
431 break; | 438 break; |
432 } | 439 } |
433 case ABS32: { | 440 case ABS32: { |
434 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); | 441 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); |
435 if (!encoded->AddAbs32(label->index_)) | 442 if (!encoded->AddAbs32(label->index_)) |
436 return NULL; | 443 return NULL; |
437 break; | 444 break; |
438 } | 445 } |
(...skipping 20 matching lines...) Expand all Loading... |
459 } | 466 } |
460 default: { | 467 default: { |
461 NOTREACHED() << "Unknown Insn OP kind"; | 468 NOTREACHED() << "Unknown Insn OP kind"; |
462 } | 469 } |
463 } | 470 } |
464 } | 471 } |
465 | 472 |
466 return encoded.release(); | 473 return encoded.release(); |
467 } | 474 } |
468 | 475 |
469 Instruction* AssemblyProgram::GetByteInstruction(uint8 byte) { | 476 Instruction* AssemblyProgram::GetByteInstruction(uint8_t byte) { |
470 if (!byte_instruction_cache_) { | 477 if (!byte_instruction_cache_) { |
471 Instruction** ram = nullptr; | 478 Instruction** ram = nullptr; |
472 if (!base::UncheckedMalloc(sizeof(Instruction*) * 256, | 479 if (!base::UncheckedMalloc(sizeof(Instruction*) * 256, |
473 reinterpret_cast<void**>(&ram))) { | 480 reinterpret_cast<void**>(&ram))) { |
474 return nullptr; | 481 return nullptr; |
475 } | 482 } |
476 byte_instruction_cache_.reset(ram); | 483 byte_instruction_cache_.reset(ram); |
477 | 484 |
478 for (int i = 0; i < 256; ++i) { | 485 for (int i = 0; i < 256; ++i) { |
479 byte_instruction_cache_[i] = | 486 byte_instruction_cache_[i] = |
480 UncheckedNew<ByteInstruction>(static_cast<uint8>(i)); | 487 UncheckedNew<ByteInstruction>(static_cast<uint8_t>(i)); |
481 if (!byte_instruction_cache_[i]) { | 488 if (!byte_instruction_cache_[i]) { |
482 for (int j = 0; j < i; ++j) | 489 for (int j = 0; j < i; ++j) |
483 UncheckedDelete(byte_instruction_cache_[j]); | 490 UncheckedDelete(byte_instruction_cache_[j]); |
484 byte_instruction_cache_.reset(); | 491 byte_instruction_cache_.reset(); |
485 return nullptr; | 492 return nullptr; |
486 } | 493 } |
487 } | 494 } |
488 } | 495 } |
489 | 496 |
490 return byte_instruction_cache_[byte]; | 497 return byte_instruction_cache_[byte]; |
(...skipping 28 matching lines...) Expand all Loading... |
519 | 526 |
520 // Walk through the list of instructions, replacing trimmed labels | 527 // Walk through the list of instructions, replacing trimmed labels |
521 // with the original machine instruction | 528 // with the original machine instruction |
522 for (size_t i = 0; i < instructions_.size(); ++i) { | 529 for (size_t i = 0; i < instructions_.size(); ++i) { |
523 Instruction* instruction = instructions_[i]; | 530 Instruction* instruction = instructions_[i]; |
524 switch (instruction->op()) { | 531 switch (instruction->op()) { |
525 case REL32ARM: { | 532 case REL32ARM: { |
526 Label* label = | 533 Label* label = |
527 static_cast<InstructionWithLabelARM*>(instruction)->label(); | 534 static_cast<InstructionWithLabelARM*>(instruction)->label(); |
528 if (label->count_ <= lower_limit) { | 535 if (label->count_ <= lower_limit) { |
529 const uint8* arm_op = | 536 const uint8_t* arm_op = |
530 static_cast<InstructionWithLabelARM*>(instruction)->arm_op(); | 537 static_cast<InstructionWithLabelARM*>(instruction)->arm_op(); |
531 uint16 op_size = | 538 uint16_t op_size = |
532 static_cast<InstructionWithLabelARM*>(instruction)->op_size(); | 539 static_cast<InstructionWithLabelARM*>(instruction)->op_size(); |
533 | 540 |
534 if (op_size < 1) | 541 if (op_size < 1) |
535 return false; | 542 return false; |
536 UncheckedDelete(instruction); | 543 UncheckedDelete(instruction); |
537 instructions_[i] = UncheckedNew<BytesInstruction>(arm_op, op_size); | 544 instructions_[i] = UncheckedNew<BytesInstruction>(arm_op, op_size); |
538 if (!instructions_[i]) | 545 if (!instructions_[i]) |
539 return false; | 546 return false; |
540 } | 547 } |
541 break; | 548 break; |
(...skipping 20 matching lines...) Expand all Loading... |
562 EncodedProgram *encoded = program->Encode(); | 569 EncodedProgram *encoded = program->Encode(); |
563 if (encoded) { | 570 if (encoded) { |
564 *output = encoded; | 571 *output = encoded; |
565 return C_OK; | 572 return C_OK; |
566 } else { | 573 } else { |
567 return C_GENERAL_ERROR; | 574 return C_GENERAL_ERROR; |
568 } | 575 } |
569 } | 576 } |
570 | 577 |
571 } // namespace courgette | 578 } // namespace courgette |
OLD | NEW |