From 86e652f8284d44c0e3fa9b27421a671e89bfe0a0 Mon Sep 17 00:00:00 2001 From: Georgii Rymar Date: Thu, 12 Dec 2019 15:47:29 +0300 Subject: [yaml2obj] - Add a way to override sh_flags section field. Currently we have the `Flags` property that allows to set flags for a section. The problem is that it does not allow us to set an arbitrary value, because of bit fields validation under the hood. An arbitrary values can be used to test specific broken cases. We probably do not want to relax the validation, so this patch adds a `ShSize` property that allows to override the `sh_size`. It is inline with others `Sh*` properties we have already. Differential revision: https://reviews.llvm.org/D71411 --- llvm/lib/ObjectYAML/ELFEmitter.cpp | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'llvm/lib/ObjectYAML/ELFEmitter.cpp') diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp index d2e6fdfea00..69f5510fc7f 100644 --- a/llvm/lib/ObjectYAML/ELFEmitter.cpp +++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -349,6 +349,20 @@ unsigned ELFState::toSymbolIndex(StringRef S, StringRef LocSec, return Index; } +template +static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To) { + if (!From) + return; + if (From->ShFlags) + To.sh_flags = *From->ShFlags; + if (From->ShName) + To.sh_name = *From->ShName; + if (From->ShOffset) + To.sh_offset = *From->ShOffset; + if (From->ShSize) + To.sh_size = *From->ShSize; +} + template bool ELFState::initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header, StringRef SecName, @@ -370,16 +384,8 @@ bool ELFState::initImplicitHeader(ContiguousBlobAccumulator &CBA, else return false; - // Override the fields if requested. - if (YAMLSec) { - if (YAMLSec->ShName) - Header.sh_name = *YAMLSec->ShName; - if (YAMLSec->ShOffset) - Header.sh_offset = *YAMLSec->ShOffset; - if (YAMLSec->ShSize) - Header.sh_size = *YAMLSec->ShSize; - } - + // Override section fields if requested. + overrideFields(YAMLSec, Header); return true; } @@ -484,15 +490,8 @@ void ELFState::initSectionHeaders(std::vector &SHeaders, llvm_unreachable("Unknown section type"); } - // Override the fields if requested. - if (Sec) { - if (Sec->ShName) - SHeader.sh_name = *Sec->ShName; - if (Sec->ShOffset) - SHeader.sh_offset = *Sec->ShOffset; - if (Sec->ShSize) - SHeader.sh_size = *Sec->ShSize; - } + // Override section fields if requested. + overrideFields(Sec, SHeader); } } -- cgit v1.2.3