Skip to content

Commit

Permalink
swift-autolink-extract should look inside .a files.
Browse files Browse the repository at this point in the history
Previously it just gave up when it saw anything other than an ELF .o file. We
could have it ignore .a files, but we might as well do the right thing.

<rdar://problem/23045632>
  • Loading branch information
cwillmor committed Nov 10, 2015
1 parent 0ea9c29 commit 2088cd4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 31 deletions.
8 changes: 8 additions & 0 deletions test/AutolinkExtract/empty_archive.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-swiftc_driver -c %s -o %t
// RUN: rm -f %t.a
// RUN: ar cr %t.a %t
// RUN: %target-swift-autolink-extract %t.a -o - | FileCheck --check-prefix CHECK-%target-object-format %s

// REQUIRES: autolink-extract

// CHECK-elf: -lswiftCore
13 changes: 13 additions & 0 deletions test/AutolinkExtract/import_archive.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: %target-swiftc_driver -emit-library -emit-module -emit-module-path %t/empty.swiftmodule -module-name empty -module-link-name empty %S/empty.swift
// RUN: %target-swiftc_driver -c %s -I %t -o %t/import_experimental.o
// RUN: ar cr %t/import_experimental.a %t/import_experimental.o
// RUN: %target-swift-autolink-extract %t/import_experimental.a -o - | FileCheck --check-prefix CHECK-%target-object-format %s

// REQUIRES: autolink-extract

// CHECK-elf-DAG: -lswiftCore
// CHECK-elf-DAG: -lempty

import empty
95 changes: 64 additions & 31 deletions tools/driver/autolink_extract_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/ELFObjectFile.h"

Expand Down Expand Up @@ -104,6 +105,61 @@ class AutolinkExtractInvocation {
return 0;
}
};

// Look inside the binary 'Bin' and append any linker flags found in its
// ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive,
// recursively look inside all children within the archive. Return 'true' if
// there was an error, and 'false' otherwise.
static bool extractLinkerFlags(const llvm::object::Binary *Bin,
CompilerInstance &Instance,
StringRef BinaryFileName,
std::set<std::string> &LinkerFlags) {
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
// Search for the section we hold autolink entries in
for (auto &Section : ObjectFile->sections()) {
llvm::StringRef SectionName;
Section.getName(SectionName);
if (SectionName == ".swift1_autolink_entries") {
llvm::StringRef SectionData;
Section.getContents(SectionData);

// entries are null-terminated, so extract them and push them into
// the set.
llvm::SmallVector<llvm::StringRef, 4> SplitFlags;
SectionData.split(SplitFlags, llvm::StringRef("\0", 1),
-1, /*KeepEmpty=*/false);
for (const auto &Flag : SplitFlags) {
LinkerFlags.insert(Flag);
}
}
}
return false;
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
for (const auto &Child : Archive->children()) {
auto ChildBinary = Child.getAsBinary();
// FIXME: BinaryFileName below should instead be ld-style names for
// object files in archives, e.g. "foo.a(bar.o)".
if (!ChildBinary) {
Instance.getDiags().diagnose(SourceLoc(), diag::error_open_input_file,
BinaryFileName,
ChildBinary.getError().message());
return true;
}
if (extractLinkerFlags(ChildBinary->get(), Instance, BinaryFileName,
LinkerFlags)) {
return true;
}
}
return false;
} else {
Instance.getDiags().diagnose(SourceLoc(), diag::error_open_input_file,
BinaryFileName,
"Don't know how to extract from object file"
"format");
return true;
}
}

int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr) {
CompilerInstance Instance;
Expand All @@ -123,41 +179,18 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
// Store each linker flag only once
std::set<std::string> LinkerFlags;

for (const auto &ObjectFileName : Invocation.getInputFilenames()) {
auto ObjectFileOwner =
llvm::object::ObjectFile::createObjectFile(ObjectFileName);
if (!ObjectFileOwner) {
// Extract the linker flags from the objects.
for (const auto &BinaryFileName : Invocation.getInputFilenames()) {
auto BinaryOwner = llvm::object::createBinary(BinaryFileName);
if (!BinaryOwner) {
Instance.getDiags().diagnose(SourceLoc(), diag::error_open_input_file,
ObjectFileName,
ObjectFileOwner.getError().message());
BinaryFileName,
BinaryOwner.getError().message());
return 1;
}

auto ObjectFile = ObjectFileOwner->getBinary();
if (llvm::isa<llvm::object::ELFObjectFileBase>(ObjectFile)) {
// Search for the section we hold autolink entries in
for (auto &Section : ObjectFileOwner->getBinary()->sections()) {
llvm::StringRef SectionName;
Section.getName(SectionName);
if (SectionName == ".swift1_autolink_entries") {
llvm::StringRef SectionData;
Section.getContents(SectionData);

// entries are null-terminated, so extract them and push them into
// the set.
llvm::SmallVector<llvm::StringRef, 4> SplitFlags;
SectionData.split(SplitFlags, llvm::StringRef("\0", 1),
-1, /*KeepEmpty=*/false);
for (const auto &Flag : SplitFlags) {
LinkerFlags.insert(Flag);
}
}
}
} else {
Instance.getDiags().diagnose(SourceLoc(), diag::error_open_input_file,
ObjectFileName,
"Don't know how to extract from object file"
"format");
if (extractLinkerFlags(BinaryOwner->getBinary(), Instance, BinaryFileName,
LinkerFlags)) {
return 1;
}
}
Expand Down

0 comments on commit 2088cd4

Please sign in to comment.