1 //: Global variables.
  2 //:
  3 //: Global variables are just labels in the data segment.
  4 //: However, they can only be used in imm32 and not disp32 operands. And they
  5 //: can't be used with jump and call instructions.
  6 //:
  7 //: This layer has much the same structure as rewriting labels.
  8 
  9 :(scenario global_variable)
 10 == code
 11 b9/copy x/imm32  # copy to ECX
 12 == data
 13 x:
 14 00 00 00 00
 15 +transform: global variable 'x' is at address 0x08049079
 16 
 17 :(before "End Level-2 Transforms")
 18 Transform.push_back(rewrite_global_variables);
 19 :(code)
 20 void rewrite_global_variables(program& p) {
 21   trace(99, "transform") << "-- rewrite global variables" << end();
 22   map<string, uint32_t> address;
 23   compute_addresses_for_global_variables(p, address);
 24   if (trace_contains_errors()) return;
 25   drop_global_variables(p);
 26   replace_global_variables_with_addresses(p, address);
 27 }
 28 
 29 void compute_addresses_for_global_variables(const program& p, map<string, uint32_t>& address) {
 30   for (int i = /*skip code segment*/1;  i < SIZE(p.segments);  ++i)
 31     compute_addresses_for_global_variables(p.segments.at(i), address);
 32 }
 33 
 34 void compute_addresses_for_global_variables(const segment& s, map<string, uint32_t>& address) {
 35   int current_address = s.start;
 36   for (int i = 0;  i < SIZE(s.lines);  ++i) {
 37     const line& inst = s.lines.at(i);
 38     for (int j = 0;  j < SIZE(inst.words);  ++j) {
 39       const word& curr = inst.words.at(j);
 40       if (*curr.data.rbegin() != ':') {
 41         ++current_address;
 42       }
 43       else {
 44         string variable = drop_last(curr.data);
 45         // ensure variables look sufficiently different from raw hex
 46         check_valid_name(variable);
 47         if (trace_contains_errors()) return;
 48         if (j > 0)
 49           raise << "'" << to_string(inst) << "': global variable names can only be the first word in a line.\n" << end();
 50         put(address, variable, current_address);
 51         trace(99, "transform") << "global variable '" << variable << "' is at address 0x" << HEXWORD << current_address << end();
 52         // no modifying current_address; global variable definitions won't be in the final binary
 53       }
 54     }
 55   }
 56 }
 57 
 58 void drop_global_variables(program& p) {
 59   for (int i = /*skip code segment*/1;  i < SIZE(p.segments);  ++i)
 60     drop_labels(p.segments.at(i));
 61 }
 62 
 63 void replace_global_variables_with_addresses(program& p, const map<string, uint32_t>& address) {
 64   if (p.segments.empty()) return;
 65   segment& code = p.segments.at(0);
 66   for (int i = 0;  i < SIZE(code.lines);  ++i) {
 67     line& inst = code.lines.at(i);
 68     line new_inst;
 69     for (int j = 0;  j < SIZE(inst.words);  ++j) {
 70       const word& curr = inst.words.at(j);
 71       if (!contains_key(address, curr.data)) {
 72         if (!looks_like_hex_int(curr.data))
 73           raise << "missing reference to global '" << curr.data << "'\n" << end();
 74         new_inst.words.push_back(curr);
 75         continue;
 76       }
 77       if (!valid_use_of_global_variable(curr)) {
 78         raise << "'" << to_string(inst) << "': can't refer to global variable '" << curr.data << "'\n" << end();
 79         return;
 80       }
 81       emit_hex_bytes(new_inst, get(address, curr.data), 4);
 82     }
 83     inst.words.swap(new_inst.words);
 84     trace(99, "transform") << "instruction after transform: '" << data_to_string(inst) << "'" << end();
 85   }
 86 }
 87 
 88 bool valid_use_of_global_variable(const word& curr) {
 89   if (has_operand_metadata(curr, "imm32")) return true;
 90   // End Valid Uses Of Global Variable(curr)
 91   return false;
 92 }
 93 
 94 //:: a more complex sanity check for how we use global variables
 95 //: requires first saving some data early before we pack operands
 96 
 97 :(after "Begin Level-2 Transforms")
 98 Transform.push_back(correlate_disp32_with_mod);
 99 :(code)
100 void correlate_disp32_with_mod(program& p) {
101   if (p.segments.empty()) return;
102   segment& code = p.segments.at(0);
103   for (int i = 0;  i < SIZE(code.lines);  ++i) {
104     line& inst = code.lines.at(i);
105     for (int j = 0;  j < SIZE(inst.words);  ++j) {
106       word& curr = inst.words.at(j);
107       if (has_operand_metadata(curr, "disp32")
108           && has_operand_metadata(inst, "mod"))
109         curr.metadata.push_back("has_mod");
110     }
111   }
112 }
113 
114 :(before "End Valid Uses Of Global Variable(curr)")
115 if (has_operand_metadata(curr, "disp32"))
116   return has_metadata(curr, "has_mod");
117 // todo: more sophisticated check, to ensure we don't use global variable
118 // addresses as a real displacement added to other operands.
119 
120 :(code)
121 bool has_metadata(const word& w, const string& m) {
122   for (int i = 0;  i < SIZE(w.metadata);  ++i)
123     if (w.metadata.at(i) == m) return true;
124   return false;
125 }
126 
127 :(scenario global_variable_disallowed_in_jump)
128 % Hide_errors = true;
129 == code
130 eb/jump x/disp8
131 == data
132 x:
133 00 00 00 00
134 +error: 'eb/jump x/disp8': can't refer to global variable 'x'
135 # sub-optimal error message; should be
136 #? +error: can't jump to data (variable 'x')
137 
138 :(scenario global_variable_disallowed_in_call)
139 % Hide_errors = true;
140 == code
141 e8/call x/disp32
142 == data
143 x:
144 00 00 00 00
145 +error: 'e8/call x/disp32': can't refer to global variable 'x'
146 # sub-optimal error message; should be
147 #? +error: can't call to the data segment ('x')
148 
149 :(scenario disp32_data_with_modrm)
150 == code
151 8b/copy 0/mod/indirect 5/rm32/.disp32 2/r32/EDX x/disp32
152 == data
153 x:
154 00 00 00 00
155 $error: 0
156 
157 :(scenarios transform)
158 :(scenario disp32_data_with_call)
159 == code
160 foo:
161 e8/call bar/disp32
162 bar:
163 $error: 0
164 
165 :(code)
166 string to_full_string(const line& in) {
167   ostringstream out;
168   for (int i = 0;  i < SIZE(in.words);  ++i) {
169     if (i > 0) out << ' ';
170     out << in.words.at(i).data;
171     for (int j = 0;  j < SIZE(in.words.at(i).metadata);  ++j)
172       out << '/' << in.words.at(i).metadata.at(j);
173   }
174   return out.str();
175 }