dtb.hh (009e81b16465ea457c0e63fd49fe77f47cc27a5a) | dtb.hh (bbe31b709a653884e18995a1c97cdafd7392999a) |
---|---|
1/*- 2 * Copyright (c) 2013 David Chisnall 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * --- 19 unchanged lines hidden (view full) --- 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33#ifndef _DTB_HH_ 34#define _DTB_HH_ 35#include <map> | 1/*- 2 * Copyright (c) 2013 David Chisnall 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * --- 19 unchanged lines hidden (view full) --- 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33#ifndef _DTB_HH_ 34#define _DTB_HH_ 35#include <map> |
36#include "string.hh" | 36#include <string> |
37 38#include <assert.h> 39 | 37 38#include <assert.h> 39 |
40#include "input_buffer.hh" 41#include "util.hh" 42 |
|
40namespace dtc 41{ 42/** 43 * The dtb namespace contains code related to the generation of device tree 44 * blobs, the binary representation of flattened device trees. The abstract 45 * tree representation calls into this code to generate the output. 46 */ 47namespace dtb --- 68 unchanged lines hidden (view full) --- 116 */ 117struct output_writer 118{ 119 /** 120 * Writes a label into the output stream. This is only applicable for 121 * assembly output, where the labels become symbols that can be 122 * resolved at link time. 123 */ | 43namespace dtc 44{ 45/** 46 * The dtb namespace contains code related to the generation of device tree 47 * blobs, the binary representation of flattened device trees. The abstract 48 * tree representation calls into this code to generate the output. 49 */ 50namespace dtb --- 68 unchanged lines hidden (view full) --- 119 */ 120struct output_writer 121{ 122 /** 123 * Writes a label into the output stream. This is only applicable for 124 * assembly output, where the labels become symbols that can be 125 * resolved at link time. 126 */ |
124 virtual void write_label(string name) = 0; | 127 virtual void write_label(const std::string &name) = 0; |
125 /** 126 * Writes a comment into the output stream. Useful only when debugging 127 * the output. 128 */ | 128 /** 129 * Writes a comment into the output stream. Useful only when debugging 130 * the output. 131 */ |
129 virtual void write_comment(string name) = 0; | 132 virtual void write_comment(const std::string &name) = 0; |
130 /** 131 * Writes a string. A nul terminator is implicitly added. 132 */ | 133 /** 134 * Writes a string. A nul terminator is implicitly added. 135 */ |
133 virtual void write_string(string name) = 0; | 136 virtual void write_string(const std::string &name) = 0; |
134 /** 135 * Writes a single 8-bit value. 136 */ 137 virtual void write_data(uint8_t) = 0; 138 /** 139 * Writes a single 32-bit value. The value is written in big-endian 140 * format, but should be passed in the host's native endian. 141 */ --- 39 unchanged lines hidden (view full) --- 181 * constructed. 182 */ 183 byte_buffer buffer; 184 public: 185 /** 186 * The binary format does not support labels, so this method 187 * does nothing. 188 */ | 137 /** 138 * Writes a single 8-bit value. 139 */ 140 virtual void write_data(uint8_t) = 0; 141 /** 142 * Writes a single 32-bit value. The value is written in big-endian 143 * format, but should be passed in the host's native endian. 144 */ --- 39 unchanged lines hidden (view full) --- 184 * constructed. 185 */ 186 byte_buffer buffer; 187 public: 188 /** 189 * The binary format does not support labels, so this method 190 * does nothing. 191 */ |
189 virtual void write_label(string) {} | 192 virtual void write_label(const std::string &) {} |
190 /** 191 * Comments are ignored by the binary writer. 192 */ | 193 /** 194 * Comments are ignored by the binary writer. 195 */ |
193 virtual void write_comment(string) {} 194 virtual void write_string(string name); | 196 virtual void write_comment(const std::string&) {} 197 virtual void write_string(const std::string &name); |
195 virtual void write_data(uint8_t v); 196 virtual void write_data(uint32_t v); 197 virtual void write_data(uint64_t v); 198 virtual void write_to_file(int fd); 199 virtual uint32_t size(); 200}; 201/** 202 * Assembly writer. This class is responsible for writing the output in an --- 16 unchanged lines hidden (view full) --- 219 int byte_count; 220 /** 221 * The current number of bytes written. This is the number in binary 222 * format, not the number of bytes in the buffer. 223 */ 224 uint32_t bytes_written; 225 226 /** | 198 virtual void write_data(uint8_t v); 199 virtual void write_data(uint32_t v); 200 virtual void write_data(uint64_t v); 201 virtual void write_to_file(int fd); 202 virtual uint32_t size(); 203}; 204/** 205 * Assembly writer. This class is responsible for writing the output in an --- 16 unchanged lines hidden (view full) --- 222 int byte_count; 223 /** 224 * The current number of bytes written. This is the number in binary 225 * format, not the number of bytes in the buffer. 226 */ 227 uint32_t bytes_written; 228 229 /** |
227 * Writes a C string directly to the output as-is. This is mainly used 228 * for writing directives. | 230 * Writes a string directly to the output as-is. This is the function that 231 * performs the real output. |
229 */ 230 void write_string(const char *c); 231 /** | 232 */ 233 void write_string(const char *c); 234 /** |
235 * Write a string to the output. 236 */ 237 void write_string(const std::string &c); 238 /** |
|
232 * Writes the string, starting on a new line. 233 */ 234 void write_line(const char *c); 235 /** 236 * Writes a byte in binary format. This will emit a single .byte 237 * directive, with up to four per line. 238 */ 239 void write_byte(uint8_t b); 240 public: 241 asm_writer() : byte_count(0), bytes_written(0) {} | 239 * Writes the string, starting on a new line. 240 */ 241 void write_line(const char *c); 242 /** 243 * Writes a byte in binary format. This will emit a single .byte 244 * directive, with up to four per line. 245 */ 246 void write_byte(uint8_t b); 247 public: 248 asm_writer() : byte_count(0), bytes_written(0) {} |
242 virtual void write_label(string name); 243 virtual void write_comment(string name); 244 virtual void write_string(string name); | 249 virtual void write_label(const std::string &name); 250 virtual void write_comment(const std::string &name); |
245 virtual void write_data(uint8_t v); 246 virtual void write_data(uint32_t v); 247 virtual void write_data(uint64_t v); 248 virtual void write_to_file(int fd); 249 virtual uint32_t size(); 250}; 251 252/** --- 70 unchanged lines hidden (view full) --- 323 * 324 * Note: We don't currently do suffix matching, which may save a small amount 325 * of space. 326 */ 327class string_table { 328 /** 329 * Map from strings to their offset. 330 */ | 251 virtual void write_data(uint8_t v); 252 virtual void write_data(uint32_t v); 253 virtual void write_data(uint64_t v); 254 virtual void write_to_file(int fd); 255 virtual uint32_t size(); 256}; 257 258/** --- 70 unchanged lines hidden (view full) --- 329 * 330 * Note: We don't currently do suffix matching, which may save a small amount 331 * of space. 332 */ 333class string_table { 334 /** 335 * Map from strings to their offset. 336 */ |
331 std::map<string, uint32_t> string_offsets; | 337 std::map<std::string, uint32_t> string_offsets; |
332 /** 333 * The strings, in the order in which they should be written to the 334 * output. The order must be stable - adding another string must not 335 * change the offset of any that we have already referenced - and so we 336 * simply write the strings in the order that they are passed. 337 */ | 338 /** 339 * The strings, in the order in which they should be written to the 340 * output. The order must be stable - adding another string must not 341 * change the offset of any that we have already referenced - and so we 342 * simply write the strings in the order that they are passed. 343 */ |
338 std::vector<string> strings; | 344 std::vector<std::string> strings; |
339 /** 340 * The current size of the strings section. 341 */ 342 uint32_t size; 343 public: 344 /** 345 * Default constructor, creates an empty strings table. 346 */ 347 string_table() : size(0) {} 348 /** 349 * Adds a string to the table, returning the offset from the start 350 * where it will be written. If the string is already present, this 351 * will return its existing offset, otherwise it will return a new 352 * offset. 353 */ | 345 /** 346 * The current size of the strings section. 347 */ 348 uint32_t size; 349 public: 350 /** 351 * Default constructor, creates an empty strings table. 352 */ 353 string_table() : size(0) {} 354 /** 355 * Adds a string to the table, returning the offset from the start 356 * where it will be written. If the string is already present, this 357 * will return its existing offset, otherwise it will return a new 358 * offset. 359 */ |
354 uint32_t add_string(string str); | 360 uint32_t add_string(const std::string &str); |
355 /** 356 * Writes the strings table to the specified output. 357 */ 358 void write(dtb::output_writer &writer); 359}; 360 361} // namespace dtb 362 363} // namespace dtc 364 365#endif // !_DTB_HH_ | 361 /** 362 * Writes the strings table to the specified output. 363 */ 364 void write(dtb::output_writer &writer); 365}; 366 367} // namespace dtb 368 369} // namespace dtc 370 371#endif // !_DTB_HH_ |