CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
156 lines
5.2 KiB
C++
156 lines
5.2 KiB
C++
//===- WasmObjcopy.cpp ----------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/ObjCopy/wasm/WasmObjcopy.h"
|
|
#include "WasmObject.h"
|
|
#include "WasmReader.h"
|
|
#include "WasmWriter.h"
|
|
#include "llvm/ObjCopy/CommonConfig.h"
|
|
#include "llvm/Support/Errc.h"
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
|
|
|
namespace llvm {
|
|
namespace objcopy {
|
|
namespace wasm {
|
|
|
|
using namespace object;
|
|
using SectionPred = std::function<bool(const Section &Sec)>;
|
|
|
|
static bool isDebugSection(const Section &Sec) {
|
|
return Sec.Name.starts_with(".debug") || Sec.Name.starts_with("reloc..debug");
|
|
}
|
|
|
|
static bool isEngineInterpretedSection(const Section &Sec) {
|
|
return Sec.SectionType != llvm::wasm::WASM_SEC_CUSTOM ||
|
|
Sec.Name.starts_with("metadata.code.");
|
|
}
|
|
|
|
static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
|
|
StringRef InputFilename, Object &Obj) {
|
|
for (const Section &Sec : Obj.Sections) {
|
|
if (Sec.Name == SecName) {
|
|
ArrayRef<uint8_t> Contents = Sec.Contents;
|
|
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
|
FileOutputBuffer::create(Filename, Contents.size());
|
|
if (!BufferOrErr)
|
|
return createFileError(Filename, BufferOrErr.takeError());
|
|
std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
|
|
llvm::copy(Contents, Buf->getBufferStart());
|
|
if (Error E = Buf->commit())
|
|
return createFileError(Filename, std::move(E));
|
|
return Error::success();
|
|
}
|
|
}
|
|
return createFileError(Filename, errc::invalid_argument,
|
|
"section '%s' not found", SecName.str().c_str());
|
|
}
|
|
|
|
static void removeSections(const CommonConfig &Config, Object &Obj) {
|
|
SectionPred RemovePred = [](const Section &) { return false; };
|
|
|
|
// Explicitly-requested sections.
|
|
if (!Config.ToRemove.empty()) {
|
|
RemovePred = [&Config](const Section &Sec) {
|
|
return Config.ToRemove.matches(Sec.Name);
|
|
};
|
|
}
|
|
|
|
if (Config.StripDebug) {
|
|
RemovePred = [RemovePred](const Section &Sec) {
|
|
return RemovePred(Sec) || isDebugSection(Sec);
|
|
};
|
|
}
|
|
|
|
if (Config.StripAll) {
|
|
RemovePred = [RemovePred](const Section &Sec) {
|
|
return RemovePred(Sec) || !isEngineInterpretedSection(Sec);
|
|
};
|
|
}
|
|
|
|
if (Config.OnlyKeepDebug) {
|
|
RemovePred = [&Config](const Section &Sec) {
|
|
// Keep debug sections, unless explicitly requested to remove.
|
|
// Remove everything else, including known sections.
|
|
return Config.ToRemove.matches(Sec.Name) || !isDebugSection(Sec);
|
|
};
|
|
}
|
|
|
|
if (!Config.OnlySection.empty()) {
|
|
RemovePred = [&Config](const Section &Sec) {
|
|
// Explicitly keep these sections regardless of previous removes.
|
|
// Remove everything else, inluding known sections.
|
|
return !Config.OnlySection.matches(Sec.Name);
|
|
};
|
|
}
|
|
|
|
if (!Config.KeepSection.empty()) {
|
|
RemovePred = [&Config, RemovePred](const Section &Sec) {
|
|
// Explicitly keep these sections regardless of previous removes.
|
|
if (Config.KeepSection.matches(Sec.Name))
|
|
return false;
|
|
// Otherwise defer to RemovePred.
|
|
return RemovePred(Sec);
|
|
};
|
|
}
|
|
|
|
Obj.removeSections(RemovePred);
|
|
}
|
|
|
|
static Error handleArgs(const CommonConfig &Config, Object &Obj) {
|
|
// Only support AddSection, DumpSection, RemoveSection for now.
|
|
for (StringRef Flag : Config.DumpSection) {
|
|
StringRef SecName;
|
|
StringRef FileName;
|
|
std::tie(SecName, FileName) = Flag.split("=");
|
|
if (Error E =
|
|
dumpSectionToFile(SecName, FileName, Config.InputFilename, Obj))
|
|
return E;
|
|
}
|
|
|
|
removeSections(Config, Obj);
|
|
|
|
for (const NewSectionInfo &NewSection : Config.AddSection) {
|
|
Section Sec;
|
|
Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM;
|
|
Sec.Name = NewSection.SectionName;
|
|
|
|
llvm::StringRef InputData =
|
|
llvm::StringRef(NewSection.SectionData->getBufferStart(),
|
|
NewSection.SectionData->getBufferSize());
|
|
std::unique_ptr<MemoryBuffer> BufferCopy = MemoryBuffer::getMemBufferCopy(
|
|
InputData, NewSection.SectionData->getBufferIdentifier());
|
|
Sec.Contents = ArrayRef<uint8_t>(
|
|
reinterpret_cast<const uint8_t *>(BufferCopy->getBufferStart()),
|
|
BufferCopy->getBufferSize());
|
|
|
|
Obj.addSectionWithOwnedContents(Sec, std::move(BufferCopy));
|
|
}
|
|
|
|
return Error::success();
|
|
}
|
|
|
|
Error executeObjcopyOnBinary(const CommonConfig &Config, const WasmConfig &,
|
|
object::WasmObjectFile &In, raw_ostream &Out) {
|
|
Reader TheReader(In);
|
|
Expected<std::unique_ptr<Object>> ObjOrErr = TheReader.create();
|
|
if (!ObjOrErr)
|
|
return createFileError(Config.InputFilename, ObjOrErr.takeError());
|
|
Object *Obj = ObjOrErr->get();
|
|
assert(Obj && "Unable to deserialize Wasm object");
|
|
if (Error E = handleArgs(Config, *Obj))
|
|
return E;
|
|
Writer TheWriter(*Obj, Out);
|
|
if (Error E = TheWriter.write())
|
|
return createFileError(Config.OutputFilename, std::move(E));
|
|
return Error::success();
|
|
}
|
|
|
|
} // end namespace wasm
|
|
} // end namespace objcopy
|
|
} // end namespace llvm
|