Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 1 | //===- Core/YamlWriter.cpp - Writes YAML ----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lld/Core/YamlWriter.h" |
Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame^] | 11 | #include "YamlKeyValues.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 12 | #include "lld/Core/Atom.h" |
| 13 | #include "lld/Core/File.h" |
Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame^] | 14 | #include "lld/Core/Platform.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 15 | #include "lld/Core/Reference.h" |
| 16 | |
Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame^] | 17 | #include "llvm/ADT/ArrayRef.h" |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/OwningPtr.h" |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/DataTypes.h" |
Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Format.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 26 | #include "llvm/Support/system_error.h" |
| 27 | |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace lld { |
| 31 | namespace yaml { |
| 32 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | /// |
| 35 | /// In most cases, atoms names are unambiguous, so references can just |
| 36 | /// use the atom name as the target (e.g. target: foo). But in a few |
| 37 | /// cases that does not work, so ref-names are added. These are labels |
| 38 | /// used only in yaml. The labels do not exist in the Atom model. |
| 39 | /// |
| 40 | /// One need for ref-names are when atoms have no user supplied name |
| 41 | /// (e.g. c-string literal). Another case is when two object files with |
| 42 | /// identically named static functions are merged (ld -r) into one object file. |
| 43 | /// In that case referencing the function by name is ambiguous, so a unique |
| 44 | /// ref-name is added. |
| 45 | /// |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 46 | class RefNameBuilder { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 47 | public: |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 48 | RefNameBuilder(const File& file) |
| 49 | : _collisionCount(0), _unnamedCounter(0) { |
| 50 | // visit all atoms |
| 51 | for(File::defined_iterator it=file.definedAtomsBegin(), |
| 52 | end=file.definedAtomsEnd(); |
| 53 | it != end; ++it) { |
| 54 | const DefinedAtom* atom = *it; |
| 55 | // Build map of atoms names to detect duplicates |
| 56 | if ( ! atom->name().empty() ) |
| 57 | buildDuplicateNameMap(*atom); |
| 58 | |
| 59 | // Find references to unnamed atoms and create ref-names for them. |
| 60 | for (auto rit=atom->referencesBegin(), rend=atom->referencesEnd(); |
| 61 | rit != rend; ++rit) { |
| 62 | const Reference* ref = *rit; |
| 63 | // create refname for any unnamed reference target |
| 64 | if ( ref->target()->name().empty() ) { |
Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 65 | std::string Storage; |
| 66 | llvm::raw_string_ostream Buffer(Storage); |
| 67 | Buffer << llvm::format("L%03d", _unnamedCounter++); |
| 68 | _refNames[ref->target()] = Buffer.str(); |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 71 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 72 | for(File::undefined_iterator it=file.undefinedAtomsBegin(), |
| 73 | end=file.undefinedAtomsEnd(); |
| 74 | it != end; ++it) { |
| 75 | buildDuplicateNameMap(**it); |
| 76 | } |
| 77 | for(File::shared_library_iterator it=file.sharedLibraryAtomsBegin(), |
| 78 | end=file.sharedLibraryAtomsEnd(); |
| 79 | it != end; ++it) { |
| 80 | buildDuplicateNameMap(**it); |
| 81 | } |
| 82 | for(File::absolute_iterator it=file.absoluteAtomsBegin(), |
| 83 | end=file.absoluteAtomsEnd(); |
| 84 | it != end; ++it) { |
| 85 | buildDuplicateNameMap(**it); |
| 86 | } |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 87 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 88 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 89 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 90 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 91 | void buildDuplicateNameMap(const Atom& atom) { |
| 92 | assert(!atom.name().empty()); |
| 93 | NameToAtom::iterator pos = _nameMap.find(atom.name()); |
| 94 | if ( pos != _nameMap.end() ) { |
| 95 | // Found name collision, give each a unique ref-name. |
Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 96 | std::string Storage; |
| 97 | llvm::raw_string_ostream Buffer(Storage); |
| 98 | Buffer << atom.name() << llvm::format(".%03d", ++_collisionCount); |
| 99 | _refNames[&atom] = Buffer.str(); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 100 | const Atom* prevAtom = pos->second; |
| 101 | AtomToRefName::iterator pos2 = _refNames.find(prevAtom); |
| 102 | if ( pos2 == _refNames.end() ) { |
| 103 | // only create ref-name for previous if none already created |
Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 104 | Buffer << prevAtom->name() << llvm::format(".%03d", ++_collisionCount); |
| 105 | _refNames[prevAtom] = Buffer.str(); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | else { |
| 109 | // First time we've seen this name, just add it to map. |
| 110 | _nameMap[atom.name()] = &atom; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool hasRefName(const Atom* atom) { |
| 115 | return _refNames.count(atom); |
| 116 | } |
| 117 | |
Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 118 | llvm::StringRef refName(const Atom* atom) { |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 119 | return _refNames.find(atom)->second; |
| 120 | } |
| 121 | |
| 122 | private: |
Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 123 | typedef llvm::StringMap<const Atom*> NameToAtom; |
| 124 | typedef llvm::DenseMap<const Atom*, std::string> AtomToRefName; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 125 | |
| 126 | unsigned int _collisionCount; |
| 127 | unsigned int _unnamedCounter; |
| 128 | NameToAtom _nameMap; |
| 129 | AtomToRefName _refNames; |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | /// |
| 134 | /// Helper class for writeObjectText() to write out atoms in yaml format. |
| 135 | /// |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 136 | class AtomWriter { |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 137 | public: |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 138 | AtomWriter(const File& file, Platform& platform, RefNameBuilder& rnb) |
| 139 | : _file(file), _platform(platform), _rnb(rnb), _firstAtom(true) { } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 140 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 141 | |
| 142 | void write(llvm::raw_ostream& out) { |
| 143 | // write header |
| 144 | out << "---\n"; |
| 145 | |
| 146 | // visit all atoms |
| 147 | for(File::defined_iterator it=_file.definedAtomsBegin(), |
| 148 | end=_file.definedAtomsEnd(); |
| 149 | it != end; ++it) { |
| 150 | writeDefinedAtom(**it, out); |
| 151 | } |
| 152 | for(File::undefined_iterator it=_file.undefinedAtomsBegin(), |
| 153 | end=_file.undefinedAtomsEnd(); |
| 154 | it != end; ++it) { |
| 155 | writeUndefinedAtom(**it, out); |
| 156 | } |
| 157 | for(File::shared_library_iterator it=_file.sharedLibraryAtomsBegin(), |
| 158 | end=_file.sharedLibraryAtomsEnd(); |
| 159 | it != end; ++it) { |
| 160 | writeSharedLibraryAtom(**it, out); |
| 161 | } |
| 162 | for(File::absolute_iterator it=_file.absoluteAtomsBegin(), |
| 163 | end=_file.absoluteAtomsEnd(); |
| 164 | it != end; ++it) { |
| 165 | writeAbsoluteAtom(**it, out); |
| 166 | } |
| 167 | |
| 168 | out << "...\n"; |
| 169 | } |
| 170 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 171 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 172 | void writeDefinedAtom(const DefinedAtom &atom, llvm::raw_ostream& out) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 173 | if ( _firstAtom ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 174 | out << "atoms:\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 175 | _firstAtom = false; |
| 176 | } |
| 177 | else { |
| 178 | // add blank line between atoms for readability |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 179 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 180 | } |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 181 | |
| 182 | bool hasDash = false; |
| 183 | if ( !atom.name().empty() ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 184 | out << " - " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 185 | << KeyValues::nameKeyword |
| 186 | << ":" |
| 187 | << spacePadding(KeyValues::nameKeyword) |
| 188 | << atom.name() |
| 189 | << "\n"; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 190 | hasDash = true; |
| 191 | } |
| 192 | |
| 193 | if ( _rnb.hasRefName(&atom) ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 194 | out << (hasDash ? " " : " - ") |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 195 | << KeyValues::refNameKeyword |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 196 | << ":" |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 197 | << spacePadding(KeyValues::refNameKeyword) |
| 198 | << _rnb.refName(&atom) |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 199 | << "\n"; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 200 | hasDash = true; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 201 | } |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 202 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 203 | if ( atom.definition() != KeyValues::definitionDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 204 | out << (hasDash ? " " : " - ") |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 205 | << KeyValues::definitionKeyword |
| 206 | << ":" |
| 207 | << spacePadding(KeyValues::definitionKeyword) |
| 208 | << KeyValues::definition(atom.definition()) |
| 209 | << "\n"; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 210 | hasDash = true; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if ( atom.scope() != KeyValues::scopeDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 214 | out << (hasDash ? " " : " - ") |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 215 | << KeyValues::scopeKeyword |
| 216 | << ":" |
| 217 | << spacePadding(KeyValues::scopeKeyword) |
| 218 | << KeyValues::scope(atom.scope()) |
| 219 | << "\n"; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 220 | hasDash = true; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 223 | if ( atom.interposable() != KeyValues::interposableDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 224 | out << " " |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 225 | << KeyValues::interposableKeyword |
| 226 | << ":" |
| 227 | << spacePadding(KeyValues::interposableKeyword) |
| 228 | << KeyValues::interposable(atom.interposable()) |
| 229 | << "\n"; |
| 230 | } |
| 231 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 232 | if ( atom.merge() != KeyValues::mergeDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 233 | out << " " |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 234 | << KeyValues::mergeKeyword |
| 235 | << ":" |
| 236 | << spacePadding(KeyValues::mergeKeyword) |
| 237 | << KeyValues::merge(atom.merge()) |
| 238 | << "\n"; |
| 239 | } |
| 240 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 241 | if ( atom.contentType() != KeyValues::contentTypeDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 242 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 243 | << KeyValues::contentTypeKeyword |
| 244 | << ":" |
| 245 | << spacePadding(KeyValues::contentTypeKeyword) |
| 246 | << KeyValues::contentType(atom.contentType()) |
| 247 | << "\n"; |
| 248 | } |
| 249 | |
| 250 | if ( atom.deadStrip() != KeyValues::deadStripKindDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 251 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 252 | << KeyValues::deadStripKindKeyword |
| 253 | << ":" |
| 254 | << spacePadding(KeyValues::deadStripKindKeyword) |
| 255 | << KeyValues::deadStripKind(atom.deadStrip()) |
| 256 | << "\n"; |
| 257 | } |
| 258 | |
| 259 | if ( atom.sectionChoice() != KeyValues::sectionChoiceDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 260 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 261 | << KeyValues::sectionChoiceKeyword |
| 262 | << ":" |
| 263 | << spacePadding(KeyValues::sectionChoiceKeyword) |
| 264 | << KeyValues::sectionChoice(atom.sectionChoice()) |
| 265 | << "\n"; |
| 266 | assert( ! atom.customSectionName().empty() ); |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 267 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 268 | << KeyValues::sectionNameKeyword |
| 269 | << ":" |
| 270 | << spacePadding(KeyValues::sectionNameKeyword) |
| 271 | << atom.customSectionName() |
| 272 | << "\n"; |
| 273 | } |
| 274 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 275 | if ( atom.isThumb() != KeyValues::isThumbDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 276 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 277 | << KeyValues::isThumbKeyword |
| 278 | << ":" |
| 279 | << spacePadding(KeyValues::isThumbKeyword) |
| 280 | << KeyValues::isThumb(atom.isThumb()) |
| 281 | << "\n"; |
| 282 | } |
| 283 | |
| 284 | if ( atom.isAlias() != KeyValues::isAliasDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 285 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 286 | << KeyValues::isAliasKeyword |
| 287 | << ":" |
| 288 | << spacePadding(KeyValues::isAliasKeyword) |
| 289 | << KeyValues::isAlias(atom.isAlias()) |
| 290 | << "\n"; |
| 291 | } |
| 292 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 293 | if ( (atom.contentType() != DefinedAtom::typeZeroFill) |
| 294 | && (atom.size() != 0) ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 295 | out << " " |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 296 | << KeyValues::contentKeyword |
| 297 | << ":" |
| 298 | << spacePadding(KeyValues::contentKeyword) |
| 299 | << "[ "; |
| 300 | llvm::ArrayRef<uint8_t> arr = atom.rawContent(); |
| 301 | bool needComma = false; |
| 302 | for (unsigned int i=0; i < arr.size(); ++i) { |
| 303 | if ( needComma ) |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 304 | out << ", "; |
Nick Kledzik | f4e2c73 | 2012-03-15 23:36:24 +0000 | [diff] [blame] | 305 | if ( ((i % 12) == 0) && (i != 0) ) { |
| 306 | out << "\n "; |
| 307 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 308 | out << hexdigit(arr[i] >> 4); |
| 309 | out << hexdigit(arr[i] & 0x0F); |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 310 | needComma = true; |
| 311 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 312 | out << " ]\n"; |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 315 | bool wroteFirstFixup = false; |
| 316 | for (auto it=atom.referencesBegin(), end=atom.referencesEnd(); |
| 317 | it != end; ++it) { |
| 318 | const Reference* ref = *it; |
| 319 | if ( !wroteFirstFixup ) { |
| 320 | out << " fixups:\n"; |
| 321 | wroteFirstFixup = true; |
| 322 | } |
| 323 | out << " - " |
| 324 | << KeyValues::fixupsOffsetKeyword |
| 325 | << ":" |
| 326 | << spacePadding(KeyValues::fixupsOffsetKeyword) |
| 327 | << ref->offsetInAtom() |
| 328 | << "\n"; |
| 329 | out << " " |
| 330 | << KeyValues::fixupsKindKeyword |
| 331 | << ":" |
| 332 | << spacePadding(KeyValues::fixupsKindKeyword) |
| 333 | << _platform.kindToString(ref->kind()) |
| 334 | << "\n"; |
| 335 | const Atom* target = ref->target(); |
| 336 | if ( target != NULL ) { |
| 337 | llvm::StringRef refName = target->name(); |
| 338 | if ( _rnb.hasRefName(target) ) |
| 339 | refName = _rnb.refName(target); |
| 340 | assert(!refName.empty()); |
| 341 | out << " " |
| 342 | << KeyValues::fixupsTargetKeyword |
| 343 | << ":" |
| 344 | << spacePadding(KeyValues::fixupsTargetKeyword) |
| 345 | << refName |
| 346 | << "\n"; |
| 347 | } |
| 348 | if ( ref->addend() != 0 ) { |
| 349 | out << " " |
| 350 | << KeyValues::fixupsAddendKeyword |
| 351 | << ":" |
| 352 | << spacePadding(KeyValues::fixupsAddendKeyword) |
| 353 | << ref->addend() |
| 354 | << "\n"; |
| 355 | } |
| 356 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 357 | } |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 358 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 359 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 360 | void writeUndefinedAtom(const UndefinedAtom &atom, llvm::raw_ostream& out) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 361 | if ( _firstAtom ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 362 | out << "atoms:\n"; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 363 | _firstAtom = false; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 364 | } |
| 365 | else { |
| 366 | // add blank line between atoms for readability |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 367 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 368 | } |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 369 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 370 | out << " - " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 371 | << KeyValues::nameKeyword |
| 372 | << ":" |
| 373 | << spacePadding(KeyValues::nameKeyword) |
| 374 | << atom.name() |
| 375 | << "\n"; |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 376 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 377 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 378 | << KeyValues::definitionKeyword |
| 379 | << ":" |
| 380 | << spacePadding(KeyValues::definitionKeyword) |
| 381 | << KeyValues::definition(atom.definition()) |
| 382 | << "\n"; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 383 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 384 | if ( atom.canBeNull() != KeyValues::canBeNullDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 385 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 386 | << KeyValues::canBeNullKeyword |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 387 | << ":" |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 388 | << spacePadding(KeyValues::canBeNullKeyword) |
| 389 | << KeyValues::canBeNull(atom.canBeNull()) |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 390 | << "\n"; |
| 391 | } |
| 392 | } |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 393 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 394 | void writeSharedLibraryAtom(const SharedLibraryAtom& atom, llvm::raw_ostream& out) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 395 | if ( _firstAtom ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 396 | out << "atoms:\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 397 | _firstAtom = false; |
| 398 | } |
| 399 | else { |
| 400 | // add blank line between atoms for readability |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 401 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 404 | out << " - " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 405 | << KeyValues::nameKeyword |
| 406 | << ":" |
| 407 | << spacePadding(KeyValues::nameKeyword) |
| 408 | << atom.name() |
| 409 | << "\n"; |
| 410 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 411 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 412 | << KeyValues::definitionKeyword |
| 413 | << ":" |
| 414 | << spacePadding(KeyValues::definitionKeyword) |
| 415 | << KeyValues::definition(atom.definition()) |
| 416 | << "\n"; |
| 417 | |
| 418 | if ( !atom.loadName().empty() ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 419 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 420 | << KeyValues::loadNameKeyword |
| 421 | << ":" |
| 422 | << spacePadding(KeyValues::loadNameKeyword) |
| 423 | << atom.loadName() |
| 424 | << "\n"; |
| 425 | } |
| 426 | |
| 427 | if ( atom.canBeNullAtRuntime() ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 428 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 429 | << KeyValues::canBeNullKeyword |
| 430 | << ":" |
| 431 | << spacePadding(KeyValues::canBeNullKeyword) |
| 432 | << KeyValues::canBeNull(UndefinedAtom::canBeNullAtRuntime) |
| 433 | << "\n"; |
| 434 | } |
| 435 | } |
| 436 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 437 | void writeAbsoluteAtom(const AbsoluteAtom& atom, llvm::raw_ostream& out) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 438 | if ( _firstAtom ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 439 | out << "atoms:\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 440 | _firstAtom = false; |
| 441 | } |
| 442 | else { |
| 443 | // add blank line between atoms for readability |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 444 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 447 | out << " - " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 448 | << KeyValues::nameKeyword |
| 449 | << ":" |
| 450 | << spacePadding(KeyValues::nameKeyword) |
| 451 | << atom.name() |
| 452 | << "\n"; |
| 453 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 454 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 455 | << KeyValues::definitionKeyword |
| 456 | << ":" |
| 457 | << spacePadding(KeyValues::definitionKeyword) |
| 458 | << KeyValues::definition(atom.definition()) |
| 459 | << "\n"; |
| 460 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 461 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 462 | << KeyValues::valueKeyword |
| 463 | << ":" |
| 464 | << spacePadding(KeyValues::valueKeyword) |
| 465 | << "0x"; |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 466 | out.write_hex(atom.value()); |
| 467 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 470 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 471 | private: |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 472 | // return a string of the correct number of spaces to align value |
| 473 | const char* spacePadding(const char* key) { |
| 474 | const char* spaces = " "; |
| 475 | assert(strlen(spaces) > strlen(key)); |
| 476 | return &spaces[strlen(key)]; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 479 | char hexdigit(uint8_t nibble) { |
| 480 | if ( nibble < 0x0A ) |
| 481 | return '0' + nibble; |
| 482 | else |
| 483 | return 'A' + nibble - 0x0A; |
| 484 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 485 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 486 | const File& _file; |
| 487 | Platform& _platform; |
| 488 | RefNameBuilder& _rnb; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 489 | bool _firstAtom; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 490 | }; |
| 491 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 492 | } // anonymous namespace |
| 493 | |
| 494 | |
| 495 | |
| 496 | /// |
| 497 | /// writeObjectText - writes the lld::File object as in YAML |
| 498 | /// format to the specified stream. |
| 499 | /// |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 500 | void writeObjectText(const File& file, Platform& platform, |
| 501 | llvm::raw_ostream &out) { |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 502 | // Figure what ref-name labels are needed |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 503 | RefNameBuilder rnb(file); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 504 | |
| 505 | // Write out all atoms |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 506 | AtomWriter writer(file, platform, rnb); |
| 507 | writer.write(out); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | } // namespace yaml |
| 511 | } // namespace lld |