9#include "../linker/image.h"
19template <
typename ... Ts>
29 return values == std::tuple<Ts...>();
45 std::map<offset_t, std::string>
names;
79 static std::shared_ptr<ChoiceDisplay> Make(std::map<offset_t, std::string>
names, std::shared_ptr<Display<offset_t>>
secondary_display)
92 static std::shared_ptr<ChoiceDisplay>
Make(std::string on_true, std::string on_false)
94 std::map<offset_t, std::string>
names;
101 return std::make_shared<ChoiceDisplay>(
names,
"",
false, 0,
nullptr);
107 static std::shared_ptr<ChoiceDisplay>
Make(std::string on_true)
109 std::map<offset_t, std::string>
names;
116 return std::make_shared<ChoiceDisplay>(
names,
"unknown",
false, 0,
nullptr);
119 bool IsMissing(std::tuple<offset_t>& values)
override;
120 void DisplayValue(
Dumper& dump, std::tuple<offset_t> values)
override;
135 static std::shared_ptr<HexDisplay> Make(
unsigned width = 8)
137 return std::make_shared<HexDisplay>(width);
140 void DisplayValue(
Dumper& dump, std::tuple<offset_t> values)
override;
150 bool enable_signed =
false;
151 DecDisplay(std::string suffix,
bool enable_signed)
152 : suffix(suffix), enable_signed(enable_signed)
156 static std::shared_ptr<DecDisplay> Make(
bool enable_signed)
158 return std::make_shared<DecDisplay>(
"", enable_signed);
161 static std::shared_ptr<DecDisplay> Make(std::string suffix =
"",
bool enable_signed =
false)
163 return std::make_shared<DecDisplay>(suffix, enable_signed);
166 void DisplayValue(
Dumper& dump, std::tuple<offset_t> values)
override;
181 static std::shared_ptr<SegmentedDisplay> Make(
unsigned width = 4)
183 return std::make_shared<SegmentedDisplay>(width);
186 void DisplayValue(
Dumper& dump, std::tuple<offset_t, offset_t> values)
override;
195 std::string separator;
197 : separator(separator)
201 static std::shared_ptr<VersionDisplay> Make(std::string separator =
".")
203 return std::make_shared<VersionDisplay>(separator);
206 void DisplayValue(
Dumper& dump, std::tuple<offset_t, offset_t> values)
override;
212template <
typename ... Ts>
217 std::shared_ptr<
Display<Ts...>> offset_display;
220 : suffix(suffix), offset_display(offset_display)
224 static std::shared_ptr<SectionedDisplay> Make(std::shared_ptr<
Display<Ts...>> offset_display)
226 return Make(
"", offset_display);
229 static std::shared_ptr<SectionedDisplay> Make(std::string suffix, std::shared_ptr<
Display<Ts...>> offset_display)
231 return std::make_shared<SectionedDisplay>(suffix, offset_display);
234 void DisplayValue(
Dumper& dump, std::tuple<offset_t, Ts...> values)
override;
246 unsigned offset, length;
247 std::shared_ptr<Display<offset_t>> display;
251 BitField(
unsigned offset,
unsigned length, std::shared_ptr<
Display<offset_t>> display,
bool optional_field, std::string label)
252 : offset(offset), length(length), display(display), optional_field(optional_field), label(label)
256 bool ShouldDisplay(std::tuple<offset_t>& values)
258 return !optional_field || !display->IsMissing(values);
262 std::map<unsigned, std::unique_ptr<BitField>> bitfields;
269 static std::shared_ptr<BitFieldDisplay> Make(
unsigned width = 8)
271 return std::make_shared<BitFieldDisplay>(width);
274 std::shared_ptr<BitFieldDisplay> AddBitField(
unsigned offset,
unsigned length, std::shared_ptr<Display<offset_t>> display,
bool optional_field)
276 bitfields[offset] = std::make_unique<BitField>(offset, length, display, optional_field,
"");
277 return shared_from_this();
280 std::shared_ptr<BitFieldDisplay> AddBitField(
unsigned offset,
unsigned length, std::string label, std::shared_ptr<Display<offset_t>> display,
bool optional_field =
false)
282 bitfields[offset] = std::make_unique<BitField>(offset, length, display, optional_field, label);
283 return shared_from_this();
286 void DisplayValue(Dumper& dump, std::tuple<offset_t> values)
override;
299 std::string open_quote, close_quote;
302 :
width(
width), open_quote(open_quote), close_quote(close_quote)
306 static std::shared_ptr<StringDisplay> Make(
size_t width, std::string open_quote, std::string close_quote)
308 return std::make_shared<StringDisplay>(
width, open_quote, close_quote);
311 static std::shared_ptr<StringDisplay> Make(
size_t width, std::string quote =
"")
313 return std::make_shared<StringDisplay>(
width, quote, quote);
316 static std::shared_ptr<StringDisplay> Make(std::string quote =
"")
318 return std::make_shared<StringDisplay>(-1, quote, quote);
321 bool IsMissing(std::tuple<std::string>& values)
override;
322 void DisplayValue(Dumper& dump, std::tuple<std::string> values)
override;
324 using Display<std::string>::IsMissing;
325 bool IsMissing(std::tuple<offset_t>& values);
326 using Display<std::string>::DisplayValue;
327 void DisplayValue(Dumper& dump, std::tuple<offset_t> values);
350 virtual bool ShouldDisplay() = 0;
351 virtual void DisplayValue(
Dumper& dump) = 0;
357template <
typename ... Ts>
363 std::tuple<Ts...> values;
370 bool ShouldDisplay()
override
375 void DisplayValue(Dumper& dump)
override
377 display->DisplayValue(dump, values);
389 std::map<std::string, std::shared_ptr<Field>> field_names;
390 std::vector<std::shared_ptr<Field>> fields;
399 std::shared_ptr<Field> FindField(std::string name)
401 auto it = field_names.find(name);
402 if(it == field_names.end())
407 template <
typename T>
408 T GetField(std::string name, offset_t default_value = T())
410 auto it = field_names.find(name);
411 if(it == field_names.end())
412 return default_value;
413 if(
auto field = std::dynamic_pointer_cast<
FieldOf<T>>(it->second))
415 return std::get<0>(field->values);
417 return default_value;
421 offset_t GetField(std::string name,
int index, offset_t default_value)
423 auto it = field_names.find(name);
424 if(it == field_names.end())
425 return default_value;
426 return it->second->values[index];
430 void AddField(std::shared_ptr<Field> field)
432 fields.push_back(field);
433 field_names[field->label] = field;
436 void AddField(
size_t index, std::shared_ptr<Field> field)
438 fields.insert(fields.begin() + index, field);
439 field_names[field->label] = field;
442 template <
typename D,
typename ... Ts>
443 void AddField(std::string label, std::shared_ptr<D> display, Ts... values)
445 AddField(std::make_shared<
FieldOf<Ts...>>(label, display, values...,
false,
false));
448 template <
typename D,
typename ... Ts>
449 void AddOptionalField(std::string label, std::shared_ptr<D> display, Ts... values)
451 AddField(std::make_shared<
FieldOf<Ts...>>(label, display, values...,
true,
false));
454 template <
typename D,
typename ... Ts>
455 void AddHiddenField(std::string label, std::shared_ptr<D> display, Ts... values)
457 AddField(std::make_shared<
FieldOf<Ts...>>(label, display, values...,
false,
true));
460 template <
typename D,
typename ... Ts>
461 void InsertField(
size_t index, std::string label, std::shared_ptr<D> display, Ts... values)
463 AddField(index, std::make_shared<
FieldOf<Ts...>>(label, display, values...,
false,
false));
466 template <
typename D,
typename ... Ts>
467 void InsertOptionalField(
size_t index, std::string label, std::shared_ptr<D> display, Ts... values)
469 AddField(index, std::make_shared<
FieldOf<Ts...>>(label, display, values...,
true,
false));
472 template <
typename D,
typename ... Ts>
473 void InsertHiddenField(
size_t index, std::string label, std::shared_ptr<D> display, Ts... values)
475 AddField(index, std::make_shared<
FieldOf<Ts...>>(label, display, values...,
false,
true));
487 Region(std::string name, offset_t offset, offset_t length,
unsigned display_width)
490 AddField(
"Offset", HexDisplay::Make(display_width), offset);
491 AddField(
"Length", HexDisplay::Make(display_width), length);
494 static std::shared_ptr<Region> Make(std::string name, offset_t offset, offset_t length,
unsigned display_width)
496 return std::make_shared<Region>(name, offset, length, display_width);
510 unsigned display_width;
512 Entry(std::string name, offset_t number, offset_t offset = offset_t(-1),
unsigned display_width = 8)
513 :
Container(name), number(number), offset(offset), display_width(display_width)
533 static char32_t encoding_default[256];
534 static char32_t encoding_cp437[256];
535 static char32_t encoding_st[256];
536 static char32_t encoding_iso8859_1[256];
537 static char32_t encoding_windows1252[256];
539 std::shared_ptr<Linker::ActualImage> image;
541 std::set<offset_t> signal_starts;
542 std::set<offset_t> signal_ends;
552 offset_t end = off + len - 1;
553 signal_starts.insert(off);
554 signal_ends.insert(end);
557 Block(std::string name, offset_t offset, std::shared_ptr<Linker::ActualImage> image, offset_t address,
unsigned display_width,
559 :
Region(name, offset, image ? image->ImageSize() : 0, display_width),
566 AddField(
"Address", HexDisplay::Make(display_width), address);
569 static std::shared_ptr<Block> Make(std::string name, offset_t offset, std::shared_ptr<Linker::ActualImage> image, offset_t address,
unsigned display_width,
575 void Display(Dumper& dump)
override;
591 char32_t (* encoding)[256];
594 : out(out), use_ansi(
true), encoding(
nullptr)
598 void SetEncoding(
char32_t (& encoding)[256],
bool force =
false)
600 if(this->encoding ==
nullptr || force)
602 this->encoding = &encoding;
606 void SetTitle(std::string title)
608 out <<
"=== " << title <<
" ===" << std::endl;
616 void PrintHex(offset_t value,
unsigned width, std::string prefix =
"0x")
618 out << prefix << std::hex << std::setw(width) << std::setfill(
'0') << value;
625 void PrintHex(offset_t value,
unsigned width,
bool prefixed)
627 PrintHex(value, width, prefixed ?
"0x" :
"");
634 void PrintDec(offset_t value, std::string prefix =
"#")
636 out << prefix << std::dec << value;
644 out << prefix << std::dec << value;
651 void PrintDec(offset_t value,
bool prefixed)
653 PrintDec(value, prefixed ?
"#" :
"");
669 out.put((c >> 6) | 0xC0);
670 out.put((c & 0x3F) | 0x80);
674 out.put((c >> 12) | 0xE0);
675 out.put(((c >> 6) & 0x3F) | 0x80);
676 out.put((c & 0x3F) | 0x80);
680 out.put(((c >> 18) & 0x07) | 0xF0);
681 out.put(((c >> 12) & 0x3F) | 0x80);
682 out.put(((c >> 6) & 0x3F) | 0x80);
683 out.put((c & 0x3F) | 0x80);
706template <
unsigned I,
size_t ... Is,
typename ... Ts>
707 inline auto rest_(std::tuple<Ts...> elements, std::index_sequence<Is...> s)
709 return std::make_tuple(std::get<I + Is>(elements)...);
712template <
unsigned I,
typename ... Ts>
713 inline auto rest(std::tuple<Ts...> elements)
715 return rest_<I>(elements, std::make_index_sequence<
sizeof...(Ts) - I>());
718template <
typename ... Ts>
719 void SectionedDisplay<Ts...>::DisplayValue(Dumper& dump, std::tuple<offset_t, Ts...> values)
721 dump.PrintDec(std::get<0>(values),
"");
722 dump.out << suffix <<
':';
723 offset_display->DisplayValue(dump, rest<1>(values));
A value that is separated into bitfields, typically bit flags.
Definition dumper.h:241
A region within a file that can be dumped, decompiled, and it may contain fixups.
Definition dumper.h:524
unsigned offset_display_width
Displaying in-file offsets.
Definition dumper.h:527
void AddSignal(offset_t off, offset_t len)
Add a relocation inside the image block.
Definition dumper.h:550
unsigned position_display_width
Displaying in-segment positions.
Definition dumper.h:529
unsigned address_display_width
Displaying in-memory addresses.
Definition dumper.h:531
Represents an enumerated value, with named options.
Definition dumper.h:40
std::map< offset_t, std::string > names
Maps values to names.
Definition dumper.h:45
std::shared_ptr< Display< offset_t > > secondary_display
Alternative representation, appearing in parentheses after the value.
Definition dumper.h:62
static std::shared_ptr< ChoiceDisplay > Make(std::string on_true)
Creates a boolean choice that is either present with name or not present at all.
Definition dumper.h:107
bool missing_on_value
If false, any value not listed in names is missing, otherwise only missing_value is missing.
Definition dumper.h:54
static std::shared_ptr< ChoiceDisplay > Make(std::string on_true, std::string on_false)
Creates a boolean choice.
Definition dumper.h:92
std::string default_name
Name for values not contained in names.
Definition dumper.h:49
offset_t missing_value
The single missing value, only used for missing_on_value true.
Definition dumper.h:58
A record whose values should be displayed together, as a collection.
Definition dumper.h:385
Represents a field with a decimal display, usually indices into an array or similar,...
Definition dumper.h:147
This class represents an entry that can be displayed in a file dump.
Definition dumper.h:21
virtual bool IsMissing(std::tuple< Ts... > &values)
Returns true if the specified value is such that it should not be displayed.
Definition dumper.h:26
virtual void DisplayValue(Dumper &dump, std::tuple< Ts... > values)=0
Prints the value through the Dumper, different types of fields can be displayed in different ways.
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:586
void PrintDecSigned(relative_offset_t value, std::string prefix="#")
Displays a decimal value (default prefix is "#")
Definition dumper.h:642
void PrintHex(offset_t value, unsigned width, std::string prefix="0x")
Displays a hexadecimal value (default prefix is "0x")
Definition dumper.h:616
void EndUnderline()
ANSI escape sequence to remove all formatting.
Definition dumper.h:699
void PrintDec(offset_t value, std::string prefix="#")
Displays a decimal value (default prefix is "#")
Definition dumper.h:634
void BeginUnderline()
ANSI escape sequence to add underline.
Definition dumper.h:690
void PutChar(char32_t c)
Displays a Unicode character as a UTF-8 byte sequence.
Definition dumper.h:660
A brief record, such as a relocation or imported library.
Definition dumper.h:506
A typed representation of a named value within a structure.
Definition dumper.h:359
std::shared_ptr< Display< Ts... > > display
The method to show it in.
Definition dumper.h:362
A representation of a named value within a structure.
Definition dumper.h:334
std::string label
The name to be displayed.
Definition dumper.h:337
bool internal
The field should not be displayed, it is for internal use (alternatively, it can be displayed through...
Definition dumper.h:341
bool optional_field
If the field is optional, it will not be displayed for certain values.
Definition dumper.h:339
Represents a field with a hexadecimal display, typically bitfields, addresses, sizes,...
Definition dumper.h:127
A record that represents a region within the file.
Definition dumper.h:485
A display with a prefix for a section.
Definition dumper.h:214
A value displayed as a colon-separated pair, typically 8086 segmented addresses.
Definition dumper.h:173
A display for a fixed or variable length string field.
Definition dumper.h:293
offset_t width
The width of the string field, exactly this many characters will be shown, unless it is offset_t(-1),...
Definition dumper.h:298
A value displayed as a separated pair, such as a version number.
Definition dumper.h:193