2015-08-28 09:40:30 +02:00
|
|
|
//===- COFFImportFile.h - COFF short import file implementation -*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-08-28 09:40:30 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// COFF short import file is a special kind of file which contains
|
|
|
|
// only symbol names for DLL-exported symbols. This class implements
|
2017-06-02 19:53:06 +02:00
|
|
|
// exporting of Symbols to create libraries and a SymbolicFile
|
|
|
|
// interface for the file type.
|
2015-08-28 09:40:30 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_OBJECT_COFF_IMPORT_FILE_H
|
|
|
|
#define LLVM_OBJECT_COFF_IMPORT_FILE_H
|
|
|
|
|
2017-06-02 19:53:06 +02:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2015-08-28 09:40:30 +02:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-08-28 09:48:41 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-08-28 09:40:30 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace object {
|
|
|
|
|
|
|
|
class COFFImportFile : public SymbolicFile {
|
|
|
|
public:
|
|
|
|
COFFImportFile(MemoryBufferRef Source)
|
|
|
|
: SymbolicFile(ID_COFFImportFile, Source) {}
|
|
|
|
|
2017-06-29 21:35:17 +02:00
|
|
|
static bool classof(Binary const *V) { return V->isCOFFImportFile(); }
|
2015-08-28 09:40:30 +02:00
|
|
|
|
|
|
|
void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; }
|
|
|
|
|
2019-05-10 11:59:04 +02:00
|
|
|
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override {
|
2015-09-01 08:01:53 +02:00
|
|
|
if (Symb.p == 0)
|
2015-08-28 09:40:30 +02:00
|
|
|
OS << "__imp_";
|
|
|
|
OS << StringRef(Data.getBufferStart() + sizeof(coff_import_header));
|
2019-05-10 11:59:04 +02:00
|
|
|
return Error::success();
|
2015-08-28 09:40:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getSymbolFlags(DataRefImpl Symb) const override {
|
|
|
|
return SymbolRef::SF_Global;
|
|
|
|
}
|
|
|
|
|
2016-11-22 04:38:40 +01:00
|
|
|
basic_symbol_iterator symbol_begin() const override {
|
2015-08-28 09:40:30 +02:00
|
|
|
return BasicSymbolRef(DataRefImpl(), this);
|
|
|
|
}
|
|
|
|
|
2016-11-22 04:38:40 +01:00
|
|
|
basic_symbol_iterator symbol_end() const override {
|
2015-08-28 09:40:30 +02:00
|
|
|
DataRefImpl Symb;
|
2017-04-28 06:29:43 +02:00
|
|
|
Symb.p = isData() ? 1 : 2;
|
2015-08-28 09:40:30 +02:00
|
|
|
return BasicSymbolRef(Symb, this);
|
|
|
|
}
|
|
|
|
|
2015-08-28 12:27:50 +02:00
|
|
|
const coff_import_header *getCOFFImportHeader() const {
|
|
|
|
return reinterpret_cast<const object::coff_import_header *>(
|
|
|
|
Data.getBufferStart());
|
|
|
|
}
|
|
|
|
|
2015-08-28 09:40:30 +02:00
|
|
|
private:
|
2017-04-28 06:29:43 +02:00
|
|
|
bool isData() const {
|
|
|
|
return getCOFFImportHeader()->getType() == COFF::IMPORT_DATA;
|
2015-08-28 09:40:30 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-02 19:53:06 +02:00
|
|
|
struct COFFShortExport {
|
[COFF] Fix /export:foo=bar when bar is a weak alias
Summary:
When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.
Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".
To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.
Fixes PR42074
Reviewers: mstorsjo, ruiu
Subscribers: thakis, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62984
llvm-svn: 362849
2019-06-08 00:05:12 +02:00
|
|
|
/// The name of the export as specified in the .def file or on the command
|
|
|
|
/// line, i.e. "foo" in "/EXPORT:foo", and "bar" in "/EXPORT:foo=bar". This
|
|
|
|
/// may lack mangling, such as underscore prefixing and stdcall suffixing.
|
2017-06-02 19:53:06 +02:00
|
|
|
std::string Name;
|
[COFF] Fix /export:foo=bar when bar is a weak alias
Summary:
When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.
Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".
To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.
Fixes PR42074
Reviewers: mstorsjo, ruiu
Subscribers: thakis, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62984
llvm-svn: 362849
2019-06-08 00:05:12 +02:00
|
|
|
|
|
|
|
/// The external, exported name. Only non-empty when export renaming is in
|
|
|
|
/// effect, i.e. "foo" in "/EXPORT:foo=bar".
|
2017-06-02 19:53:06 +02:00
|
|
|
std::string ExtName;
|
[COFF] Fix /export:foo=bar when bar is a weak alias
Summary:
When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.
Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".
To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.
Fixes PR42074
Reviewers: mstorsjo, ruiu
Subscribers: thakis, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62984
llvm-svn: 362849
2019-06-08 00:05:12 +02:00
|
|
|
|
|
|
|
/// The real, mangled symbol name from the object file. Given
|
|
|
|
/// "/export:foo=bar", this could be "_bar@8" if bar is stdcall.
|
2017-08-16 07:13:16 +02:00
|
|
|
std::string SymbolName;
|
[COFF] Fix /export:foo=bar when bar is a weak alias
Summary:
When handling exports from the command line or from .def files, the
linker does a "fuzzy" string lookup to allow finding mangled symbols.
However, when the symbol is re-exported under a new name, the linker has
to transfer the decorations from the exported symbol over to the new
name. This is implemented by taking the mangled symbol that was found in
the object and replacing the original symbol name with the export name.
Before this patch, LLD implemented the fuzzy search by adding an
undefined symbol with the unmangled name, and then during symbol
resolution, checking if similar mangled symbols had been added after the
last round of symbol resolution. If so, LLD makes the original symbol a
weak alias of the mangled symbol. Later, to get the original symbol
name, LLD would look through the weak alias and forward it on to the
import library writer, which copies the symbol decorations. This
approach doesn't work when bar is itself a weak alias, as is the case in
asan. It's especially bad when the aliasee of bar contains the string
"bar", consider "bar_default". In this case, we would end up exporting
the symbol "foo_default" when we should've exported just "foo".
To fix this, don't look through weak aliases to find the mangled name.
Save the mangled name earlier during fuzzy symbol lookup.
Fixes PR42074
Reviewers: mstorsjo, ruiu
Subscribers: thakis, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62984
llvm-svn: 362849
2019-06-08 00:05:12 +02:00
|
|
|
|
|
|
|
/// Creates a weak alias. This is the name of the weak aliasee. In a .def
|
|
|
|
/// file, this is "baz" in "EXPORTS\nfoo = bar == baz".
|
2018-05-09 11:21:53 +02:00
|
|
|
std::string AliasTarget;
|
2017-06-02 19:53:06 +02:00
|
|
|
|
|
|
|
uint16_t Ordinal = 0;
|
|
|
|
bool Noname = false;
|
|
|
|
bool Data = false;
|
|
|
|
bool Private = false;
|
|
|
|
bool Constant = false;
|
|
|
|
|
|
|
|
friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) {
|
|
|
|
return L.Name == R.Name && L.ExtName == R.ExtName &&
|
|
|
|
L.Ordinal == R.Ordinal && L.Noname == R.Noname &&
|
|
|
|
L.Data == R.Data && L.Private == R.Private;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) {
|
|
|
|
return !(L == R);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-22 01:13:36 +02:00
|
|
|
Error writeImportLibrary(StringRef ImportName, StringRef Path,
|
|
|
|
ArrayRef<COFFShortExport> Exports,
|
2018-05-09 11:21:53 +02:00
|
|
|
COFF::MachineTypes Machine, bool MinGW);
|
2017-06-02 19:53:06 +02:00
|
|
|
|
2015-08-28 09:40:30 +02:00
|
|
|
} // namespace object
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
#endif
|