[lld] r190585 - Do not hard code the leading underscore.
Rui Ueyama
ruiu at google.com
Wed Sep 11 21:42:31 PDT 2013
Author: ruiu
Date: Wed Sep 11 23:42:31 2013
New Revision: 190585
URL: http://llvm.org/viewvc/llvm-project?rev=190585&view=rev
Log:
Do not hard code the leading underscore.
Mangling scheme varies on platform, and prepending an underscore is valid only
on 32-bit x86. Added a method to mangle name to PECOFFLinkingContext and use
it to avoid hard coding mangled names.
Modified:
lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=190585&r1=190584&r2=190585&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Wed Sep 11 23:42:31 2013
@@ -69,6 +69,17 @@ public:
StringRef searchLibraryFile(StringRef path) const;
+ /// Returns the decorated name of the given symbol name. On 32-bit x86, it
+ /// adds "_" at the beginning of the string. On other architectures, the
+ /// return value is the same as the argument.
+ StringRef decorateSymbol(StringRef name) const {
+ // Because we don't support architectures other than 32-bit x86, we'll
+ // prepend an underscore unconditionally.
+ std::string str = "_";
+ str.append(name);
+ return allocateString(str);
+ }
+
void setBaseAddress(uint64_t addr) { _baseAddress = addr; }
uint64_t getBaseAddress() const { return _baseAddress; }
Modified: lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h?rev=190585&r1=190584&r2=190585&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h Wed Sep 11 23:42:31 2013
@@ -31,18 +31,16 @@ namespace {
// The symbol __ImageBase is a linker generated symbol. No standard library
// files define it, but the linker is expected to prepare it as if it was read
// from a file. The content of the atom is a 4-byte integer equal to the image
-// base address.
-//
-// Note that because the name is prefixed by an underscore on x86 Win32, the
-// actual symbol name becomes ___ImageBase (with three underscores). On other
-// architectures names are not mangled with an underscore. We need to handle
-// name mangling correctly when porting LLD Windows code to them.
+// base address. Note that because the name is prefixed by an underscore on x86
+// Win32, the actual symbol name will be ___ImageBase (three underscores).
class ImageBaseAtom : public COFFLinkerInternalAtom {
public:
- ImageBaseAtom(const File &file, uint32_t imageBase)
- : COFFLinkerInternalAtom(file, assembleRawContent(imageBase)) {}
+ ImageBaseAtom(const PECOFFLinkingContext &context, const File &file,
+ uint32_t imageBase)
+ : COFFLinkerInternalAtom(file, assembleRawContent(imageBase)),
+ _name(context.decorateSymbol("__ImageBase")) {}
- virtual StringRef name() const { return "___ImageBase"; }
+ virtual StringRef name() const { return _name; }
virtual uint64_t ordinal() const { return 0; }
virtual ContentType contentType() const { return typeData; }
virtual ContentPermissions permissions() const { return permRW_; }
@@ -54,6 +52,8 @@ private:
*(reinterpret_cast<uint32_t *>(&data[0])) = imageBase;
return data;
}
+
+ StringRef _name;
};
// The file to wrap ImageBaseAtom. This is the only member file of
@@ -62,10 +62,14 @@ class MemberFile : public SimpleFile {
public:
MemberFile(const PECOFFLinkingContext &context)
: SimpleFile(context, "Member of the Linker Internal File"),
- _atom(*this, context.getBaseAddress()) {
+ _atom(context, *this, context.getBaseAddress()) {
addAtom(_atom);
};
+ bool contains(StringRef name) const {
+ return _atom.name() == name;
+ }
+
private:
ImageBaseAtom _atom;
};
@@ -86,7 +90,7 @@ public:
_memberFile(context) {};
virtual const File *find(StringRef name, bool dataSymbolOnly) const {
- if (name == "___ImageBase")
+ if (_memberFile.contains(name))
return &_memberFile;
return nullptr;
}
More information about the llvm-commits
mailing list