From 4c37b3e91b3e9c891c22379f029c42abc38e408c Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sun, 28 Oct 2018 13:41:53 -0700 Subject: 4734 --- html/subx/028translate.cc.html | 72 +++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 36 deletions(-) (limited to 'html/subx/028translate.cc.html') diff --git a/html/subx/028translate.cc.html b/html/subx/028translate.cc.html index ce01c630..2745d059 100644 --- a/html/subx/028translate.cc.html +++ b/html/subx/028translate.cc.html @@ -77,17 +77,17 @@ if ('onhashchange' in window) { 17 //: Higher levels usually transform code on the basis of metadata. 18 19 :(before "End Main") - 20 if (is_equal(argv[1], "translate")) { - 21 START_TRACING_UNTIL_END_OF_SCOPE; - 22 reset(); + 20 if (is_equal(argv[1], "translate")) { + 21 START_TRACING_UNTIL_END_OF_SCOPE; + 22 reset(); 23 // Begin subx translate - 24 program p; + 24 program p; 25 string output_filename; 26 for (int i = /*skip 'subx translate'*/2; i < argc; ++i) { - 27 if (is_equal(argv[i], "-o")) { + 27 if (is_equal(argv[i], "-o")) { 28 ++i; 29 if (i >= argc) { - 30 print_translate_usage(); + 30 print_translate_usage(); 31 cerr << "'-o' must be followed by a filename to write results to\n"; 32 exit(1); 33 } @@ -100,23 +100,23 @@ if ('onhashchange' in window) { 40 return 1; 41 } 42 parse(fin, p); - 43 if (trace_contains_errors()) return 1; + 43 if (trace_contains_errors()) return 1; 44 } 45 } - 46 if (p.segments.empty()) { - 47 print_translate_usage(); + 46 if (p.segments.empty()) { + 47 print_translate_usage(); 48 cerr << "nothing to do; must provide at least one file to read\n"; 49 exit(1); 50 } 51 if (output_filename.empty()) { - 52 print_translate_usage(); + 52 print_translate_usage(); 53 cerr << "must provide a filename to write to using '-o'\n"; 54 exit(1); 55 } - 56 transform(p); - 57 if (trace_contains_errors()) return 1; - 58 save_elf(p, output_filename); - 59 if (trace_contains_errors()) { + 56 transform(p); + 57 if (trace_contains_errors()) return 1; + 58 save_elf(p, output_filename); + 59 if (trace_contains_errors()) { 60 unlink(output_filename.c_str()); 61 return 1; 62 } @@ -125,20 +125,20 @@ if ('onhashchange' in window) { 65 } 66 67 :(code) - 68 void print_translate_usage() { + 68 void print_translate_usage() { 69 cerr << "Usage: subx translate file1 file2 ... -o output\n"; 70 } 71 72 // write out a program to a bare-bones ELF file - 73 void save_elf(const program& p, const string& filename) { + 73 void save_elf(const program& p, const string& filename) { 74 ofstream out(filename.c_str(), ios::binary); - 75 write_elf_header(out, p); - 76 for (size_t i = 0; i < p.segments.size(); ++i) - 77 write_segment(p.segments.at(i), out); + 75 write_elf_header(out, p); + 76 for (size_t i = 0; i < p.segments.size(); ++i) + 77 write_segment(p.segments.at(i), out); 78 out.close(); 79 } 80 - 81 void write_elf_header(ostream& out, const program& p) { + 81 void write_elf_header(ostream& out, const program& p) { 82 char c = '\0'; 83 #define O(X) c = (X); out.write(&c, sizeof(c)) 84 // host is required to be little-endian @@ -157,7 +157,7 @@ if ('onhashchange' in window) { 97 // e_version 98 O(0x01); O(0x00); O(0x00); O(0x00); 99 // e_entry -100 int e_entry = p.segments.at(0).start; // convention +100 int e_entry = p.segments.at(0).start; // convention 101 emit(e_entry); 102 // e_phoff -- immediately after ELF header 103 int e_phoff = 0x34; @@ -174,7 +174,7 @@ if ('onhashchange' in window) { 114 uint16_t e_phentsize = 0x20; 115 emit(e_phentsize); 116 // e_phnum -117 uint16_t e_phnum = SIZE(p.segments); +117 uint16_t e_phnum = SIZE(p.segments); 118 emit(e_phnum); 119 // e_shentsize 120 uint16_t dummy16 = 0x0; @@ -184,8 +184,8 @@ if ('onhashchange' in window) { 124 // e_shstrndx 125 emit(dummy16); 126 -127 uint32_t p_offset = /*size of ehdr*/0x34 + SIZE(p.segments)*0x20/*size of each phdr*/; -128 for (int i = 0; i < SIZE(p.segments); ++i) { +127 uint32_t p_offset = /*size of ehdr*/0x34 + SIZE(p.segments)*0x20/*size of each phdr*/; +128 for (int i = 0; i < SIZE(p.segments); ++i) { 129 //// phdr 130 // p_type 131 uint32_t p_type = 0x1; @@ -193,13 +193,13 @@ if ('onhashchange' in window) { 133 // p_offset 134 emit(p_offset); 135 // p_vaddr -136 uint32_t p_start = p.segments.at(i).start; +136 uint32_t p_start = p.segments.at(i).start; 137 emit(p_start); 138 // p_paddr 139 emit(p_start); 140 // p_filesz -141 uint32_t size = num_words(p.segments.at(i)); -142 assert(p_offset + size < INITIAL_SEGMENT_SIZE); +141 uint32_t size = num_words(p.segments.at(i)); +142 assert(p_offset + size < INITIAL_SEGMENT_SIZE); 143 emit(size); 144 // p_memsz 145 emit(size); @@ -221,7 +221,7 @@ if ('onhashchange' in window) { 161 uint32_t p_align = 0x1000; // default page size on linux 162 emit(p_align); 163 if (p_offset % p_align != p_start % p_align) { -164 raise << "segment starting at 0x" << HEXWORD << p_start << " is improperly aligned; alignment for p_offset " << p_offset << " should be " << (p_offset % p_align) << " but is " << (p_start % p_align) << '\n' << end(); +164 raise << "segment starting at 0x" << HEXWORD << p_start << " is improperly aligned; alignment for p_offset " << p_offset << " should be " << (p_offset % p_align) << " but is " << (p_start % p_align) << '\n' << end(); 165 return; 166 } 167 @@ -232,20 +232,20 @@ if ('onhashchange' in window) { 172 #undef emit 173 } 174 -175 void write_segment(const segment& s, ostream& out) { -176 for (int i = 0; i < SIZE(s.lines); ++i) { -177 const vector<word>& w = s.lines.at(i).words; -178 for (int j = 0; j < SIZE(w); ++j) { -179 uint8_t x = hex_byte(w.at(j).data); // we're done with metadata by this point +175 void write_segment(const segment& s, ostream& out) { +176 for (int i = 0; i < SIZE(s.lines); ++i) { +177 const vector<word>& w = s.lines.at(i).words; +178 for (int j = 0; j < SIZE(w); ++j) { +179 uint8_t x = hex_byte(w.at(j).data); // we're done with metadata by this point 180 out.write(reinterpret_cast<const char*>(&x), /*sizeof(byte)*/1); 181 } 182 } 183 } 184 -185 uint32_t num_words(const segment& s) { +185 uint32_t num_words(const segment& s) { 186 uint32_t sum = 0; -187 for (int i = 0; i < SIZE(s.lines); ++i) -188 sum += SIZE(s.lines.at(i).words); +187 for (int i = 0; i < SIZE(s.lines); ++i) +188 sum += SIZE(s.lines.at(i).words); 189 return sum; 190 } 191 -- cgit 1.4.1-2-gfad0