1 //===- Symbols.cpp --------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Symbols.h" 10 #include "Config.h" 11 #include "InputChunks.h" 12 #include "InputElement.h" 13 #include "InputFiles.h" 14 #include "OutputSections.h" 15 #include "OutputSegment.h" 16 #include "lld/Common/ErrorHandler.h" 17 #include "lld/Common/Memory.h" 18 #include "lld/Common/Strings.h" 19 20 #define DEBUG_TYPE "lld" 21 22 using namespace llvm; 23 using namespace llvm::object; 24 using namespace llvm::wasm; 25 26 namespace lld { 27 std::string toString(const wasm::Symbol &sym) { 28 return maybeDemangleSymbol(sym.getName()); 29 } 30 31 std::string maybeDemangleSymbol(StringRef name) { 32 // WebAssembly requires caller and callee signatures to match, so we mangle 33 // `main` in the case where we need to pass it arguments. 34 if (name == "__main_argc_argv") 35 return "main"; 36 if (wasm::config->demangle) 37 return demangleItanium(name); 38 return std::string(name); 39 } 40 41 std::string toString(wasm::Symbol::Kind kind) { 42 switch (kind) { 43 case wasm::Symbol::DefinedFunctionKind: 44 return "DefinedFunction"; 45 case wasm::Symbol::DefinedDataKind: 46 return "DefinedData"; 47 case wasm::Symbol::DefinedGlobalKind: 48 return "DefinedGlobal"; 49 case wasm::Symbol::DefinedTableKind: 50 return "DefinedTable"; 51 case wasm::Symbol::DefinedTagKind: 52 return "DefinedTag"; 53 case wasm::Symbol::UndefinedFunctionKind: 54 return "UndefinedFunction"; 55 case wasm::Symbol::UndefinedDataKind: 56 return "UndefinedData"; 57 case wasm::Symbol::UndefinedGlobalKind: 58 return "UndefinedGlobal"; 59 case wasm::Symbol::UndefinedTableKind: 60 return "UndefinedTable"; 61 case wasm::Symbol::LazyKind: 62 return "LazyKind"; 63 case wasm::Symbol::SectionKind: 64 return "SectionKind"; 65 case wasm::Symbol::OutputSectionKind: 66 return "OutputSectionKind"; 67 } 68 llvm_unreachable("invalid symbol kind"); 69 } 70 71 namespace wasm { 72 DefinedFunction *WasmSym::callCtors; 73 DefinedFunction *WasmSym::callDtors; 74 DefinedFunction *WasmSym::initMemory; 75 DefinedFunction *WasmSym::applyDataRelocs; 76 DefinedFunction *WasmSym::applyGlobalRelocs; 77 DefinedFunction *WasmSym::initTLS; 78 DefinedFunction *WasmSym::startFunction; 79 DefinedData *WasmSym::dsoHandle; 80 DefinedData *WasmSym::dataEnd; 81 DefinedData *WasmSym::globalBase; 82 DefinedData *WasmSym::heapBase; 83 DefinedData *WasmSym::initMemoryFlag; 84 GlobalSymbol *WasmSym::stackPointer; 85 GlobalSymbol *WasmSym::tlsBase; 86 GlobalSymbol *WasmSym::tlsSize; 87 GlobalSymbol *WasmSym::tlsAlign; 88 UndefinedGlobal *WasmSym::tableBase; 89 DefinedData *WasmSym::definedTableBase; 90 UndefinedGlobal *WasmSym::tableBase32; 91 DefinedData *WasmSym::definedTableBase32; 92 UndefinedGlobal *WasmSym::memoryBase; 93 DefinedData *WasmSym::definedMemoryBase; 94 TableSymbol *WasmSym::indirectFunctionTable; 95 96 WasmSymbolType Symbol::getWasmType() const { 97 if (isa<FunctionSymbol>(this)) 98 return WASM_SYMBOL_TYPE_FUNCTION; 99 if (isa<DataSymbol>(this)) 100 return WASM_SYMBOL_TYPE_DATA; 101 if (isa<GlobalSymbol>(this)) 102 return WASM_SYMBOL_TYPE_GLOBAL; 103 if (isa<TagSymbol>(this)) 104 return WASM_SYMBOL_TYPE_TAG; 105 if (isa<TableSymbol>(this)) 106 return WASM_SYMBOL_TYPE_TABLE; 107 if (isa<SectionSymbol>(this) || isa<OutputSectionSymbol>(this)) 108 return WASM_SYMBOL_TYPE_SECTION; 109 llvm_unreachable("invalid symbol kind"); 110 } 111 112 const WasmSignature *Symbol::getSignature() const { 113 if (auto* f = dyn_cast<FunctionSymbol>(this)) 114 return f->signature; 115 if (auto *l = dyn_cast<LazySymbol>(this)) 116 return l->signature; 117 return nullptr; 118 } 119 120 InputChunk *Symbol::getChunk() const { 121 if (auto *f = dyn_cast<DefinedFunction>(this)) 122 return f->function; 123 if (auto *f = dyn_cast<UndefinedFunction>(this)) 124 if (f->stubFunction) 125 return f->stubFunction->function; 126 if (auto *d = dyn_cast<DefinedData>(this)) 127 return d->segment; 128 return nullptr; 129 } 130 131 bool Symbol::isDiscarded() const { 132 if (InputChunk *c = getChunk()) 133 return c->discarded; 134 return false; 135 } 136 137 bool Symbol::isLive() const { 138 if (auto *g = dyn_cast<DefinedGlobal>(this)) 139 return g->global->live; 140 if (auto *t = dyn_cast<DefinedTag>(this)) 141 return t->tag->live; 142 if (auto *t = dyn_cast<DefinedTable>(this)) 143 return t->table->live; 144 if (InputChunk *c = getChunk()) 145 return c->live; 146 return referenced; 147 } 148 149 void Symbol::markLive() { 150 assert(!isDiscarded()); 151 referenced = true; 152 if (file != NULL && isDefined()) 153 file->markLive(); 154 if (auto *g = dyn_cast<DefinedGlobal>(this)) 155 g->global->live = true; 156 if (auto *t = dyn_cast<DefinedTag>(this)) 157 t->tag->live = true; 158 if (auto *t = dyn_cast<DefinedTable>(this)) 159 t->table->live = true; 160 if (InputChunk *c = getChunk()) { 161 // Usually, a whole chunk is marked as live or dead, but in mergeable 162 // (splittable) sections, each piece of data has independent liveness bit. 163 // So we explicitly tell it which offset is in use. 164 if (auto *d = dyn_cast<DefinedData>(this)) { 165 if (auto *ms = dyn_cast<MergeInputChunk>(c)) { 166 ms->getSectionPiece(d->value)->live = true; 167 } 168 } 169 c->live = true; 170 } 171 } 172 173 uint32_t Symbol::getOutputSymbolIndex() const { 174 assert(outputSymbolIndex != INVALID_INDEX); 175 return outputSymbolIndex; 176 } 177 178 void Symbol::setOutputSymbolIndex(uint32_t index) { 179 LLVM_DEBUG(dbgs() << "setOutputSymbolIndex " << name << " -> " << index 180 << "\n"); 181 assert(outputSymbolIndex == INVALID_INDEX); 182 outputSymbolIndex = index; 183 } 184 185 void Symbol::setGOTIndex(uint32_t index) { 186 LLVM_DEBUG(dbgs() << "setGOTIndex " << name << " -> " << index << "\n"); 187 assert(gotIndex == INVALID_INDEX); 188 if (config->isPic) { 189 // Any symbol that is assigned a GOT entry must be exported otherwise the 190 // dynamic linker won't be able create the entry that contains it. 191 forceExport = true; 192 } 193 gotIndex = index; 194 } 195 196 bool Symbol::isWeak() const { 197 return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK; 198 } 199 200 bool Symbol::isLocal() const { 201 return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_LOCAL; 202 } 203 204 bool Symbol::isHidden() const { 205 return (flags & WASM_SYMBOL_VISIBILITY_MASK) == WASM_SYMBOL_VISIBILITY_HIDDEN; 206 } 207 208 void Symbol::setHidden(bool isHidden) { 209 LLVM_DEBUG(dbgs() << "setHidden: " << name << " -> " << isHidden << "\n"); 210 flags &= ~WASM_SYMBOL_VISIBILITY_MASK; 211 if (isHidden) 212 flags |= WASM_SYMBOL_VISIBILITY_HIDDEN; 213 else 214 flags |= WASM_SYMBOL_VISIBILITY_DEFAULT; 215 } 216 217 bool Symbol::isExported() const { 218 if (!isDefined() || isLocal()) 219 return false; 220 221 if (config->exportAll || (config->exportDynamic && !isHidden())) 222 return true; 223 224 return isExportedExplicit(); 225 } 226 227 bool Symbol::isExportedExplicit() const { 228 return forceExport || flags & WASM_SYMBOL_EXPORTED; 229 } 230 231 bool Symbol::isNoStrip() const { 232 return flags & WASM_SYMBOL_NO_STRIP; 233 } 234 235 uint32_t FunctionSymbol::getFunctionIndex() const { 236 if (auto *f = dyn_cast<DefinedFunction>(this)) 237 return f->function->getFunctionIndex(); 238 if (const auto *u = dyn_cast<UndefinedFunction>(this)) { 239 if (u->stubFunction) { 240 return u->stubFunction->getFunctionIndex(); 241 } 242 } 243 assert(functionIndex != INVALID_INDEX); 244 return functionIndex; 245 } 246 247 void FunctionSymbol::setFunctionIndex(uint32_t index) { 248 LLVM_DEBUG(dbgs() << "setFunctionIndex " << name << " -> " << index << "\n"); 249 assert(functionIndex == INVALID_INDEX); 250 functionIndex = index; 251 } 252 253 bool FunctionSymbol::hasFunctionIndex() const { 254 if (auto *f = dyn_cast<DefinedFunction>(this)) 255 return f->function->hasFunctionIndex(); 256 return functionIndex != INVALID_INDEX; 257 } 258 259 uint32_t FunctionSymbol::getTableIndex() const { 260 if (auto *f = dyn_cast<DefinedFunction>(this)) 261 return f->function->getTableIndex(); 262 assert(tableIndex != INVALID_INDEX); 263 return tableIndex; 264 } 265 266 bool FunctionSymbol::hasTableIndex() const { 267 if (auto *f = dyn_cast<DefinedFunction>(this)) 268 return f->function->hasTableIndex(); 269 return tableIndex != INVALID_INDEX; 270 } 271 272 void FunctionSymbol::setTableIndex(uint32_t index) { 273 // For imports, we set the table index here on the Symbol; for defined 274 // functions we set the index on the InputFunction so that we don't export 275 // the same thing twice (keeps the table size down). 276 if (auto *f = dyn_cast<DefinedFunction>(this)) { 277 f->function->setTableIndex(index); 278 return; 279 } 280 LLVM_DEBUG(dbgs() << "setTableIndex " << name << " -> " << index << "\n"); 281 assert(tableIndex == INVALID_INDEX); 282 tableIndex = index; 283 } 284 285 DefinedFunction::DefinedFunction(StringRef name, uint32_t flags, InputFile *f, 286 InputFunction *function) 287 : FunctionSymbol(name, DefinedFunctionKind, flags, f, 288 function ? &function->signature : nullptr), 289 function(function) {} 290 291 uint64_t DefinedData::getVA() const { 292 LLVM_DEBUG(dbgs() << "getVA: " << getName() << "\n"); 293 if (segment) 294 return segment->getVA(value); 295 return value; 296 } 297 298 void DefinedData::setVA(uint64_t value_) { 299 LLVM_DEBUG(dbgs() << "setVA " << name << " -> " << value_ << "\n"); 300 assert(!segment); 301 value = value_; 302 } 303 304 uint64_t DefinedData::getOutputSegmentOffset() const { 305 LLVM_DEBUG(dbgs() << "getOutputSegmentOffset: " << getName() << "\n"); 306 return segment->getChunkOffset(value); 307 } 308 309 uint64_t DefinedData::getOutputSegmentIndex() const { 310 LLVM_DEBUG(dbgs() << "getOutputSegmentIndex: " << getName() << "\n"); 311 return segment->outputSeg->index; 312 } 313 314 uint32_t GlobalSymbol::getGlobalIndex() const { 315 if (auto *f = dyn_cast<DefinedGlobal>(this)) 316 return f->global->getAssignedIndex(); 317 assert(globalIndex != INVALID_INDEX); 318 return globalIndex; 319 } 320 321 void GlobalSymbol::setGlobalIndex(uint32_t index) { 322 LLVM_DEBUG(dbgs() << "setGlobalIndex " << name << " -> " << index << "\n"); 323 assert(globalIndex == INVALID_INDEX); 324 globalIndex = index; 325 } 326 327 bool GlobalSymbol::hasGlobalIndex() const { 328 if (auto *f = dyn_cast<DefinedGlobal>(this)) 329 return f->global->hasAssignedIndex(); 330 return globalIndex != INVALID_INDEX; 331 } 332 333 DefinedGlobal::DefinedGlobal(StringRef name, uint32_t flags, InputFile *file, 334 InputGlobal *global) 335 : GlobalSymbol(name, DefinedGlobalKind, flags, file, 336 global ? &global->getType() : nullptr), 337 global(global) {} 338 339 uint32_t TagSymbol::getTagIndex() const { 340 if (auto *f = dyn_cast<DefinedTag>(this)) 341 return f->tag->getAssignedIndex(); 342 assert(tagIndex != INVALID_INDEX); 343 return tagIndex; 344 } 345 346 void TagSymbol::setTagIndex(uint32_t index) { 347 LLVM_DEBUG(dbgs() << "setTagIndex " << name << " -> " << index << "\n"); 348 assert(tagIndex == INVALID_INDEX); 349 tagIndex = index; 350 } 351 352 bool TagSymbol::hasTagIndex() const { 353 if (auto *f = dyn_cast<DefinedTag>(this)) 354 return f->tag->hasAssignedIndex(); 355 return tagIndex != INVALID_INDEX; 356 } 357 358 DefinedTag::DefinedTag(StringRef name, uint32_t flags, InputFile *file, 359 InputTag *tag) 360 : TagSymbol(name, DefinedTagKind, flags, file, 361 tag ? &tag->getType() : nullptr, 362 tag ? &tag->signature : nullptr), 363 tag(tag) {} 364 365 void TableSymbol::setLimits(const WasmLimits &limits) { 366 if (auto *t = dyn_cast<DefinedTable>(this)) 367 t->table->setLimits(limits); 368 auto *newType = make<WasmTableType>(*tableType); 369 newType->Limits = limits; 370 tableType = newType; 371 } 372 373 uint32_t TableSymbol::getTableNumber() const { 374 if (const auto *t = dyn_cast<DefinedTable>(this)) 375 return t->table->getAssignedIndex(); 376 assert(tableNumber != INVALID_INDEX); 377 return tableNumber; 378 } 379 380 void TableSymbol::setTableNumber(uint32_t number) { 381 if (const auto *t = dyn_cast<DefinedTable>(this)) 382 return t->table->assignIndex(number); 383 LLVM_DEBUG(dbgs() << "setTableNumber " << name << " -> " << number << "\n"); 384 assert(tableNumber == INVALID_INDEX); 385 tableNumber = number; 386 } 387 388 bool TableSymbol::hasTableNumber() const { 389 if (const auto *t = dyn_cast<DefinedTable>(this)) 390 return t->table->hasAssignedIndex(); 391 return tableNumber != INVALID_INDEX; 392 } 393 394 DefinedTable::DefinedTable(StringRef name, uint32_t flags, InputFile *file, 395 InputTable *table) 396 : TableSymbol(name, DefinedTableKind, flags, file, 397 table ? &table->getType() : nullptr), 398 table(table) {} 399 400 const OutputSectionSymbol *SectionSymbol::getOutputSectionSymbol() const { 401 assert(section->outputSec && section->outputSec->sectionSym); 402 return section->outputSec->sectionSym; 403 } 404 405 void LazySymbol::fetch() { cast<ArchiveFile>(file)->addMember(&archiveSymbol); } 406 407 void LazySymbol::setWeak() { 408 flags |= (flags & ~WASM_SYMBOL_BINDING_MASK) | WASM_SYMBOL_BINDING_WEAK; 409 } 410 411 MemoryBufferRef LazySymbol::getMemberBuffer() { 412 Archive::Child c = 413 CHECK(archiveSymbol.getMember(), 414 "could not get the member for symbol " + toString(*this)); 415 416 return CHECK(c.getMemoryBufferRef(), 417 "could not get the buffer for the member defining symbol " + 418 toString(*this)); 419 } 420 421 void printTraceSymbolUndefined(StringRef name, const InputFile* file) { 422 message(toString(file) + ": reference to " + name); 423 } 424 425 // Print out a log message for --trace-symbol. 426 void printTraceSymbol(Symbol *sym) { 427 // Undefined symbols are traced via printTraceSymbolUndefined 428 if (sym->isUndefined()) 429 return; 430 431 std::string s; 432 if (sym->isLazy()) 433 s = ": lazy definition of "; 434 else 435 s = ": definition of "; 436 437 message(toString(sym->getFile()) + s + sym->getName()); 438 } 439 440 const char *defaultModule = "env"; 441 const char *functionTableName = "__indirect_function_table"; 442 443 } // namespace wasm 444 } // namespace lld 445