| 1 |
//===- ArgvParser.cpp - Parse the command-line options ----------*- C++ -*-===// |
| 2 |
// |
| 3 |
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
/// \file |
| 10 |
/// Parse the command-line options given to clang-extract. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
/* Author: Giuliano Belinassi */ |
| 15 |
|
| 16 |
#include "ArgvParser.hh" |
| 17 |
#include "NonLLVMMisc.hh" |
| 18 |
#include "Error.hh" |
| 19 |
|
| 20 |
#include <clang/Basic/Version.h> |
| 21 |
|
| 22 |
#ifndef CLANG_VERSION_MAJOR |
| 23 |
# error "Unable to find clang version" |
| 24 |
#endif |
| 25 |
|
| 26 |
/** List of arguments that are supported by GCC but unsupported by clang, |
| 27 |
resulting in compilation error. */ |
| 28 |
static const char *UnsupportedGCCArgs[] = { |
| 29 |
"-mpreferred-stack-boundary=", |
| 30 |
"-mindirect-branch=thunk-extern", |
| 31 |
"-mindirect-branch-register", |
| 32 |
"-mrecord-mcount", |
| 33 |
"-falign-jumps=", |
| 34 |
"-fasan-shadow-offset=", |
| 35 |
"-fconserve-stack", |
| 36 |
"-fdump-ipa-clones", |
| 37 |
"-flive-patching=inline-clone", |
| 38 |
"-fmin-function-alignment=16", |
| 39 |
"-fno-allow-store-data-races", |
| 40 |
"-fno-var-tracking-assignments", |
| 41 |
"-Wno-alloc-size-larger-than", |
| 42 |
"-Wno-dangling-pointer", |
| 43 |
"-Wno-format-truncation", |
| 44 |
"-Wno-format-overflow", |
| 45 |
"-Wmaybe-uninitialized", |
| 46 |
"-Wno-maybe-uninitialized", |
| 47 |
"-Wpacked-not-aligned", |
| 48 |
"-Wno-packed-not-aligned", |
| 49 |
"-Wno-restrict", |
| 50 |
"-Wno-stringop-overflow", |
| 51 |
"-Wstringop-truncation", |
| 52 |
"-Wno-stringop-truncation", |
| 53 |
"-Werror=designated-init", |
| 54 |
"-Wimplicit-fallthrough=", |
| 55 |
"-Wendif-labels", |
| 56 |
"-Wmissing-selector-name", |
| 57 |
"-Wfree-labels", |
| 58 |
"-Wmissing-parameter-name", |
| 59 |
}; |
| 60 |
|
| 61 |
ArgvParser::ArgvParser(int argc, char **argv) |
| 62 |
: ArgsToClang(), |
| 63 |
FunctionsToExtract(), |
| 64 |
SymbolsToExternalize(), |
| 65 |
HeadersToExpand(), |
| 66 |
HeadersToNotExpand(), |
| 67 |
OutputFile(), |
| 68 |
IgnoreClangErrors(false), |
| 69 |
DisableExternalization(false), |
| 70 |
WithIncludes(false), |
| 71 |
DumpPasses(false), |
| 72 |
RenameSymbols(false), |
| 73 |
Kernel(false), |
| 74 |
Ibt(false), |
| 75 |
AllowLateExternalization(false), |
| 76 |
PatchObject(""), |
| 77 |
Debuginfos(), |
| 78 |
IpaclonesPath(nullptr), |
| 79 |
SymversPath(nullptr), |
| 80 |
DescOutputPath(nullptr), |
| 81 |
IncExpansionPolicy(nullptr), |
| 82 |
OutputFunctionPrototypeHeader(nullptr) |
| 83 |
{ |
| 84 |
for (int i = 0; i < argc; i++) { |
| 85 |
if (!Handle_Clang_Extract_Arg(argv[i])) { |
| 86 |
ArgsToClang.push_back(argv[i]); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
Insert_Required_Parameters(); |
| 91 |
|
| 92 |
const char *DebuginfoPath = nullptr; |
| 93 |
if (Debuginfos.size() > 0) { |
| 94 |
DebuginfoPath = Debuginfos[0].c_str(); |
| 95 |
} |
| 96 |
|
| 97 |
/* For kernel, check if the object patch is not the same as DebugInfo. If they |
| 98 |
* are not the same, it means that the module from PatchObject is builtin, so |
| 99 |
* assign vmlinux to PatchObject. */ |
| 100 |
if (Kernel && DebuginfoPath) { |
| 101 |
std::string obj_path = get_basename(DebuginfoPath); |
| 102 |
/* As the DebugInfo can point to a file with suffix (btrfs.ko for example), |
| 103 |
* check the substring */ |
| 104 |
if (obj_path.find(PatchObject) == std::string::npos) |
| 105 |
PatchObject = "vmlinux"; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void ArgvParser::Insert_Required_Parameters(void) |
| 110 |
{ |
| 111 |
std::vector<const char *> priv_args = { |
| 112 |
"-Xclang", "-detailed-preprocessing-record", |
| 113 |
// For some reason libtooling do not pass the clang include folder. Pass this then. |
| 114 |
#ifdef __i386__ |
| 115 |
"-I/usr/lib/clang/" STRINGFY_VALUE(CLANG_VERSION_MAJOR) "/include", |
| 116 |
#else |
| 117 |
"-I/usr/lib64/clang/" STRINGFY_VALUE(CLANG_VERSION_MAJOR) "/include", |
| 118 |
#endif |
| 119 |
"-Wno-gnu-variable-sized-type-not-at-end", |
| 120 |
"-Wno-incompatible-pointer-types-discards-qualifiers", // this can be triggered for older codestreams |
| 121 |
"-Wno-missing-prototypes", // We remove the static keyword from the |
| 122 |
// extracted function, so disable warnings |
| 123 |
// related to it. |
| 124 |
"-Wno-unused-function", // May happen when trying to extract a static function. |
| 125 |
"-Wno-unused-variable", // Passes may instroduce unused variables later removed. |
| 126 |
"-fno-builtin", // clang interposes some glibc functions and then it fails to find the declaration of them. |
| 127 |
"-Wno-duplicate-decl-specifier", // Disabled due to kernel issues. See more |
| 128 |
// at https://github.com/ClangBuiltLinux/linux/issues/2013 |
| 129 |
// and https://github.com/llvm/llvm-project/issues/93449 |
| 130 |
}; |
| 131 |
|
| 132 |
for (const char *arg : priv_args) { |
| 133 |
ArgsToClang.push_back(arg); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void ArgvParser::Print_Usage_Message(void) |
| 138 |
{ |
| 139 |
llvm::outs() << |
| 140 |
"OVERVIEW: Tool to extract code from C projects using the LLVM infrastructure.\n" |
| 141 |
" It should be invoked as a C compiler.\n" |
| 142 |
"\n" |
| 143 |
"USAGE: clang-extract [options] file...\n" |
| 144 |
"\n" |
| 145 |
"CLANG-EXTRACT OPTIONS:\n" |
| 146 |
" <clang-switch> A clang switch, as specified by calling clang --help.\n" |
| 147 |
" -D__KERNEL__ Indicate that we are processing a Linux sourcefile.\n" |
| 148 |
" -DCE_EXTRACT_FUNCTIONS=<args>\n" |
| 149 |
" Extract the functions specified in the <args> list.\n" |
| 150 |
" -DCE_EXPORT_SYMBOLS=<args>\n" |
| 151 |
" Force externalization of symbols specified in the <args> list\n" |
| 152 |
" -DCE_OUTPUT_FILE=<arg> Output code to <arg> file. Default is <input>.CE.c.\n" |
| 153 |
" -DCE_NO_EXTERNALIZATION Disable symbol externalization.\n" |
| 154 |
" -DCE_DUMP_PASSES Dump the results of each transformation pass into files.\n" |
| 155 |
" -DCE_KEEP_INCLUDES Keep all possible #include<file> directives.\n" |
| 156 |
" -DCE_KEEP_INCLUDES=<policy>\n" |
| 157 |
" Keep all possible #include<file> directives, but using the\n" |
| 158 |
" specified include expansion <policy>. Valid values are\n" |
| 159 |
" nothing, everything, kernel, system and compiler.\n" |
| 160 |
" -DCE_EXPAND_INCLUDES=<args>\n" |
| 161 |
" Force expansion of the headers provided in <args>.\n" |
| 162 |
" -DCE_NOT_EXPAND_INCLUDES=<args>\n" |
| 163 |
" Force the following headers to NOT be expanded.\n" |
| 164 |
" -DCE_RENAME_SYMBOLS Allow renaming of extracted symbols.\n" |
| 165 |
" -DCE_DEBUGINFO_PATH=<arg>\n" |
| 166 |
" Path to the compiled (ELF) object of the desired program to\n" |
| 167 |
" extract. This is used to decide if externalization is\n" |
| 168 |
" necessary or not for given symbol.\n" |
| 169 |
" -DCE_IPACLONES_PATH=<arg>\n" |
| 170 |
" Path to .ipa-clones files generated by gcc. Used to decide\n" |
| 171 |
" if desired function to extract was inlined into other\n" |
| 172 |
" functions.\n" |
| 173 |
" -DCE_SYMVERS_PATH=<arg> Path to kernel Modules.symvers file. Only used when\n" |
| 174 |
" -D__KERNEL__ is specified.\n" |
| 175 |
" -DCE_DSC_OUTPUT=<arg> Libpulp .dsc file output, used for userspace livepatching.\n" |
| 176 |
" -DCE_OUTPUT_FUNCTION_PROTOTYPE_HEADER=<arg>\n" |
| 177 |
" Outputs a header file with a foward declaration of all\n" |
| 178 |
" functions. This header is not self-compilable.\n" |
| 179 |
" -DCE_LATE_EXTERNALIZE Enable late externalization (declare externalized variables\n" |
| 180 |
" later than the original). May reduce code output when\n" |
| 181 |
" -DCE_KEEP_INCLUDES is enabled\n" |
| 182 |
" -DCE_IGNORE_CLANG_ERRORS Ignore clang compilation errors in a hope that code is\n" |
| 183 |
" generated even if it won't compile.\n" |
| 184 |
"\n"; |
| 185 |
|
| 186 |
llvm::outs() << "The following arguments are ignored by clang-extract:\n"; |
| 187 |
for (unsigned long i = 0; i < ARRAY_LENGTH(UnsupportedGCCArgs); i++) { |
| 188 |
llvm::outs() << " " << UnsupportedGCCArgs[i] << '\n'; |
| 189 |
} |
| 190 |
|
| 191 |
llvm::outs() << '\n'; |
| 192 |
llvm::outs() << "For a list of switches accepted by clang, run:\n" << |
| 193 |
" $ clang --help\n"; |
| 194 |
} |
| 195 |
|
| 196 |
bool ArgvParser::Handle_Clang_Extract_Arg(const char *str) |
| 197 |
{ |
| 198 |
/* Ignore gcc arguments that are not known to clang */ |
| 199 |
for (unsigned long i = 0; i < ARRAY_LENGTH(UnsupportedGCCArgs); i++) { |
| 200 |
if (prefix(UnsupportedGCCArgs[i], str)) { |
| 201 |
return true; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (!strncmp("-D__KERNEL__", str, 12)) { |
| 206 |
Kernel = true; |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
if (!strcmp("-D__USE_IBT__", str)) { |
| 211 |
Ibt = true; |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
if (prefix("-DKBUILD_MODNAME=", str)) { |
| 216 |
/* Avoid storing double quotes */ |
| 217 |
PatchObject = Extract_Single_Arg(str); |
| 218 |
PatchObject.erase(std::remove(PatchObject.begin(), PatchObject.end(), '\"' ), |
| 219 |
PatchObject.end()); |
| 220 |
|
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
if (prefix("-DCE_EXTRACT_FUNCTIONS=", str)) { |
| 225 |
FunctionsToExtract = Extract_Args(str); |
| 226 |
|
| 227 |
return true; |
| 228 |
} |
| 229 |
if (prefix("-DCE_EXPORT_SYMBOLS=", str)) { |
| 230 |
SymbolsToExternalize = Extract_Args(str); |
| 231 |
|
| 232 |
return true; |
| 233 |
} |
| 234 |
if (prefix("-DCE_OUTPUT_FILE=", str)) { |
| 235 |
OutputFile = Extract_Single_Arg(str); |
| 236 |
|
| 237 |
return true; |
| 238 |
} |
| 239 |
if (!strcmp("-DCE_NO_EXTERNALIZATION", str)) { |
| 240 |
DisableExternalization = true; |
| 241 |
|
| 242 |
return true; |
| 243 |
} |
| 244 |
if (!strcmp("-DCE_DUMP_PASSES", str)) { |
| 245 |
DumpPasses = true; |
| 246 |
|
| 247 |
return true; |
| 248 |
} |
| 249 |
if (!strcmp("-DCE_KEEP_INCLUDES", str)) { |
| 250 |
WithIncludes = true; |
| 251 |
return true; |
| 252 |
} |
| 253 |
if (prefix("-DCE_KEEP_INCLUDES=", str)) { |
| 254 |
WithIncludes = true; |
| 255 |
IncExpansionPolicy = Extract_Single_Arg_C(str); |
| 256 |
return true; |
| 257 |
} |
| 258 |
if (prefix("-DCE_EXPAND_INCLUDES=", str)) { |
| 259 |
HeadersToExpand = Extract_Args(str); |
| 260 |
|
| 261 |
return true; |
| 262 |
} |
| 263 |
if (prefix("-DCE_NOT_EXPAND_INCLUDES=", str)) { |
| 264 |
HeadersToNotExpand = Extract_Args(str); |
| 265 |
|
| 266 |
return true; |
| 267 |
} |
| 268 |
if (prefix("-DCE_DEBUGINFO_PATH=", str)) { |
| 269 |
Debuginfos = Extract_Args(str); |
| 270 |
|
| 271 |
return true; |
| 272 |
} |
| 273 |
if (prefix("-DCE_IPACLONES_PATH=", str)) { |
| 274 |
IpaclonesPath = Extract_Single_Arg_C(str); |
| 275 |
|
| 276 |
return true; |
| 277 |
} |
| 278 |
if (prefix("-DCE_SYMVERS_PATH=", str)) { |
| 279 |
SymversPath = Extract_Single_Arg_C(str); |
| 280 |
|
| 281 |
return true; |
| 282 |
} |
| 283 |
if (prefix("-DCE_DSC_OUTPUT=", str)) { |
| 284 |
DescOutputPath = Extract_Single_Arg_C(str); |
| 285 |
|
| 286 |
return true; |
| 287 |
} |
| 288 |
if (prefix("-DCE_OUTPUT_FUNCTION_PROTOTYPE_HEADER=", str)) { |
| 289 |
OutputFunctionPrototypeHeader = Extract_Single_Arg_C(str); |
| 290 |
|
| 291 |
return true; |
| 292 |
} |
| 293 |
if (!strcmp("-DCE_RENAME_SYMBOLS", str)) { |
| 294 |
RenameSymbols = true; |
| 295 |
|
| 296 |
return true; |
| 297 |
} |
| 298 |
if (!strcmp("-DCE_LATE_EXTERNALIZE", str)) { |
| 299 |
AllowLateExternalization = true; |
| 300 |
|
| 301 |
return true; |
| 302 |
} |
| 303 |
if (!strcmp("-DCE_IGNORE_CLANG_ERRORS", str)) { |
| 304 |
IgnoreClangErrors = true; |
| 305 |
|
| 306 |
return true; |
| 307 |
} |
| 308 |
|
| 309 |
if (!strcmp("--help", str)) { |
| 310 |
Print_Usage_Message(); |
| 311 |
exit(0); |
| 312 |
} |
| 313 |
if (prefix("-DCE_", str)) { |
| 314 |
DiagsClass::Emit_Error("Unrecognized command-line option: " + std::string(str)); |
| 315 |
exit(1); |
| 316 |
} |
| 317 |
|
| 318 |
return false; |
| 319 |
} |