| 1 | //===-- llvm-strings.cpp - Printable String dumping utility ---------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, 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 | // This program is a utility that works like binutils "strings", that is, it |
| 10 | // prints out printable strings in a binary, objdump, or archive file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Opts.inc" |
| 15 | #include "llvm/ADT/StringExtras.h" |
| 16 | #include "llvm/Object/Binary.h" |
| 17 | #include "llvm/Option/Arg.h" |
| 18 | #include "llvm/Option/ArgList.h" |
| 19 | #include "llvm/Option/Option.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/ConvertUTF.h" |
| 22 | #include "llvm/Support/Error.h" |
| 23 | #include "llvm/Support/FileSystem.h" |
| 24 | #include "llvm/Support/Format.h" |
| 25 | #include "llvm/Support/InitLLVM.h" |
| 26 | #include "llvm/Support/Unicode.h" |
| 27 | #include "llvm/Support/UnicodeCharRanges.h" |
| 28 | #include "llvm/Support/WithColor.h" |
| 29 | #include <bitset> |
| 30 | #include <clocale> |
| 31 | #include <string> |
| 32 | |
| 33 | #ifdef LLVM_ON_UNIX |
| 34 | #include <langinfo.h> |
| 35 | #endif |
| 36 | |
| 37 | // Ideally we would use mbrtoc32() and c32rtomb() unconditionally but there are |
| 38 | // problems on most platforms, thus we introduce wrappers. |
| 39 | |
| 40 | // Use C++17 std::mbrtoc32() and std::c32rtomb() directly. |
| 41 | #define MBTOC_UCHAR 1 |
| 42 | |
| 43 | // Emulate std::mbrtoc32() and std::c32rtomb() through std::mbrtowc() and |
| 44 | // std::wcrtomb(). Requires wchar_t to hold Unicode code points. |
| 45 | #define MBTOC_WCHAR 2 |
| 46 | |
| 47 | // Emulate std::mbrtoc32() and std::c32rtomb() through iconv(). Requires knowing |
| 48 | // the right implementation-specific code set name for UTF-32. |
| 49 | #define MBTOC_ICONV 3 |
| 50 | |
| 51 | // Default to the C++17 implementation, unless we find we are on a system where |
| 52 | // we should not. |
| 53 | #define MBTOC MBTOC_UCHAR |
| 54 | |
| 55 | // glibc supports mbrtoc32() and c32rtomb() since version 2.16. However, if |
| 56 | // libstdc++ was built against an older glibc, it may not wrap its functions by |
| 57 | // default. |
| 58 | // On glibc, mbrtoc32() is a wrapper for mbrtowc() and never returns (size_t)-3, |
| 59 | // instead it handles this case by returning 0 but setting Ch to a non-null |
| 60 | // character. This is worked around later. |
| 61 | #ifdef __GLIBC__ |
| 62 | #if !__GLIBC_PREREQ(2, 16) |
| 63 | #undef MBTOC |
| 64 | #define MBTOC MBTOC_WCHAR |
| 65 | #endif |
| 66 | #endif |
| 67 | |
| 68 | // On Windows, mbrtoc32() ignores the locale and only converts UTF-8, but |
| 69 | // wchar_t is UTF-16 and is close enough, as long as we make sure to avoid it |
| 70 | // for UTF-8 locales. |
| 71 | #ifdef _WIN32 |
| 72 | #include "llvm/Support/Windows/WindowsSupport.h" |
| 73 | #undef MBTOC |
| 74 | #define MBTOC MBTOC_WCHAR |
| 75 | #endif |
| 76 | |
| 77 | // On Apple, <uchar.h> is not available, and wchar_t is not always UTF-32. We |
| 78 | // implement mbrtoc() and crtomb() through iconv(). |
| 79 | // On FreeBSD, <uchar.h> is available but the result for encoding errors is |
| 80 | // (size_t)-2 rather than (size_t)-1. We cannot recover from this, so likewise |
| 81 | // use iconv(). |
| 82 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 83 | #undef MBTOC |
| 84 | #define MBTOC MBTOC_ICONV |
| 85 | #endif |
| 86 | |
| 87 | #if MBTOC == MBTOC_UCHAR |
| 88 | // Support for <cuchar> is less widespread than <uchar.h>: if libstdc++ was |
| 89 | // configured against glibc before 2.16 (even if current glibc is newer), or if |
| 90 | // a libc++ version older than 15 is used, <cuchar> will either be nonfunctional |
| 91 | // or unavailable, even when <uchar.h> works perfectly. |
| 92 | #include <uchar.h> |
| 93 | #elif MBTOC == MBTOC_WCHAR |
| 94 | #include <cwchar> |
| 95 | #elif MBTOC == MBTOC_ICONV |
| 96 | #ifdef ICONVH |
| 97 | #include ICONVH |
| 98 | #else |
| 99 | #include <iconv.h> |
| 100 | #endif |
| 101 | #endif |
| 102 | |
| 103 | using namespace llvm; |
| 104 | using namespace llvm::object; |
| 105 | |
| 106 | namespace llvm::sys::unicode { |
| 107 | LLVM_ABI extern const UnicodeCharSet::CharRanges GeneratedCharacterRanges; |
| 108 | LLVM_ABI extern const UnicodeCharSet::CharRanges |
| 109 | GeneratedControlCharacterRanges; |
| 110 | } // namespace llvm::sys::unicode |
| 111 | |
| 112 | namespace { |
| 113 | enum ID { |
| 114 | OPT_INVALID = 0, // This is not an option ID. |
| 115 | #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__), |
| 116 | #include "Opts.inc" |
| 117 | #undef OPTION |
| 118 | }; |
| 119 | |
| 120 | using namespace llvm::opt; |
| 121 | #define OPTTABLE_CODE |
| 122 | #include "Opts.inc" |
| 123 | |
| 124 | class StringsOptTable : public opt::OptTable { |
| 125 | public: |
| 126 | StringsOptTable() : OptTable(optionTables()) { |
| 127 | setGroupedShortOptions(true); |
| 128 | setDashDashParsing(true); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | static StringRef ToolName; |
| 133 | |
| 134 | static cl::list<std::string> InputFileNames(cl::Positional, |
| 135 | cl::desc("<input object files>" )); |
| 136 | |
| 137 | static constexpr int DefaultMinLength = 4; |
| 138 | static int MinLength = DefaultMinLength; |
| 139 | static bool PrintFileName; |
| 140 | |
| 141 | enum class Encoding { Ascii, Locale, Utf8 }; |
| 142 | static Encoding Encoding; |
| 143 | |
| 144 | enum class Radix { None, Octal, Hexadecimal, Decimal }; |
| 145 | static Radix Radix; |
| 146 | |
| 147 | [[noreturn]] static void reportCmdLineError(const Twine &Message) { |
| 148 | WithColor::error(OS&: errs(), Prefix: ToolName) << Message << "\n" ; |
| 149 | exit(status: 1); |
| 150 | } |
| 151 | |
| 152 | [[noreturn]] static void invalidArgValue(Arg *Arg) { |
| 153 | reportCmdLineError(Message: "'" + StringRef(Arg->getValue()) + |
| 154 | "' is not a valid value for '" + Arg->getSpelling() + "'" ); |
| 155 | } |
| 156 | |
| 157 | template <typename T> |
| 158 | static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) { |
| 159 | if (const opt::Arg *A = Args.getLastArg(Ids: ID)) { |
| 160 | StringRef V(A->getValue()); |
| 161 | if (!llvm::to_integer(V, Value, 0) || Value <= 0) |
| 162 | reportCmdLineError(Message: "expected a positive integer, but got '" + V + "'" ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | #if MBTOC == MBTOC_UCHAR |
| 167 | using char_t = char32_t; |
| 168 | |
| 169 | static size_t mbrtoc(char_t *Ch, const char *S, size_t N, |
| 170 | std::mbstate_t *MBState) { |
| 171 | size_t Result = mbrtoc32(pc32: Ch, s: S, n: N, p: MBState); |
| 172 | if (Result == 0 && *Ch) |
| 173 | Result = (size_t)-3; |
| 174 | return Result; |
| 175 | } |
| 176 | |
| 177 | static size_t crtomb(char *S, char_t Ch, std::mbstate_t *MBState) { |
| 178 | return c32rtomb(s: S, c32: Ch, ps: MBState); |
| 179 | } |
| 180 | #elif MBTOC == MBTOC_WCHAR |
| 181 | using char_t = wchar_t; |
| 182 | |
| 183 | static size_t mbrtoc(char_t *Ch, const char *S, size_t N, |
| 184 | std::mbstate_t *MBState) { |
| 185 | size_t Result = std::mbrtowc(Ch, S, N, MBState); |
| 186 | if (Result == 0 && *Ch) |
| 187 | Result = (size_t)-3; |
| 188 | return Result; |
| 189 | } |
| 190 | |
| 191 | static size_t crtomb(char *S, char_t Ch, std::mbstate_t *MBState) { |
| 192 | return std::wcrtomb(S, Ch, MBState); |
| 193 | } |
| 194 | #elif MBTOC == MBTOC_ICONV |
| 195 | struct IconvHandle { |
| 196 | IconvHandle(const char *DstName, const char *SrcName) |
| 197 | : Value(iconv_open(DstName, SrcName)) { |
| 198 | if (Value == (iconv_t)-1) |
| 199 | reportCmdLineError("failed to open iconv" ); |
| 200 | } |
| 201 | IconvHandle(IconvHandle &&) = delete; |
| 202 | ~IconvHandle() { iconv_close(Value); } |
| 203 | iconv_t value() const { return Value; } |
| 204 | |
| 205 | private: |
| 206 | iconv_t Value; |
| 207 | }; |
| 208 | |
| 209 | using char_t = char32_t; |
| 210 | |
| 211 | static size_t mbrtoc(char_t *Ch, const char *S, size_t N, |
| 212 | std::mbstate_t *MBState) { |
| 213 | static IconvHandle CD("UCS-4-INTERNAL" , nl_langinfo(CODESET)); |
| 214 | |
| 215 | wchar_t WCh; |
| 216 | size_t BytesRead = mbrtowc(&WCh, S, N, MBState); |
| 217 | if (BytesRead != (size_t)-2 && BytesRead != (size_t)-1) { |
| 218 | char Cvt[MB_LEN_MAX]; |
| 219 | std::mbstate_t CvtMBState{}; |
| 220 | const size_t BytesWritten = wcrtomb(Cvt, WCh, &CvtMBState); |
| 221 | char *PIn = Cvt; |
| 222 | size_t NIn = BytesWritten; |
| 223 | char *POut = reinterpret_cast<char *>(Ch); |
| 224 | size_t NOut = sizeof(char_t); |
| 225 | // This is not generally correct for all systems, but this is limited to |
| 226 | // Apple and FreeBSD. Neither supports stateful encodings in its locales, so |
| 227 | // we do not need to worry about them in the call to iconv(). Beware if this |
| 228 | // needs to be adapted to run on other systems. |
| 229 | const size_t NumErrors = iconv(CD.value(), &PIn, &NIn, &POut, &NOut); |
| 230 | // mbrtowc() may be lenient and accept invalid characters that iconv() |
| 231 | // rejects. |
| 232 | if (NumErrors || NIn || NOut) |
| 233 | return (size_t)-1; |
| 234 | } |
| 235 | return BytesRead; |
| 236 | } |
| 237 | |
| 238 | static size_t crtomb(char *S, char_t Ch, std::mbstate_t *MBState) { |
| 239 | static IconvHandle CD(nl_langinfo(CODESET), "UCS-4-INTERNAL" ); |
| 240 | |
| 241 | char Cvt[MB_LEN_MAX]; |
| 242 | std::mbstate_t CvtMBState{}; |
| 243 | char *PIn = reinterpret_cast<char *>(&Ch); |
| 244 | size_t NIn = sizeof(char_t); |
| 245 | char *POut = Cvt; |
| 246 | size_t NOut = MB_LEN_MAX; |
| 247 | const size_t NumErrors = iconv(CD.value(), &PIn, &NIn, &POut, &NOut); |
| 248 | if (NumErrors == (size_t)-1 || NIn) |
| 249 | llvm_unreachable("internal error: iconv failed on valid character" ); |
| 250 | if (NumErrors) |
| 251 | return (size_t)-1; |
| 252 | wchar_t WCh; |
| 253 | if (mbrtowc(&WCh, Cvt, POut - Cvt, &CvtMBState) != size_t(POut - Cvt)) |
| 254 | llvm_unreachable("internal error: mbrtowc failed on valid character" ); |
| 255 | return wcrtomb(S, WCh, MBState); |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | struct CharSet { |
| 260 | template <typename T> struct Base { |
| 261 | using CharT = T; |
| 262 | |
| 263 | // Tries to read one character from the bytes Cur...End and store it in Ch. |
| 264 | // Returns true if a (possibly invalid) character was read, false otherwise. |
| 265 | // |
| 266 | // If Cur...End starts with a valid complete character, Cur and MBState are |
| 267 | // updated and true is returned. |
| 268 | // If Cur...End is empty, or holds an incomplete character and AtEOF is |
| 269 | // false, Cur and MBState are unchanged, Ch is set to 0, and false is |
| 270 | // returned. This is intended to allow more bytes to be read and readChar to |
| 271 | // be called again. If Cur...End holds an incomplete character and AtEOF is |
| 272 | // true, or if Cur...End starts with an invalid character, the first bytes |
| 273 | // are skipped and MBState is reset to allow continuing from the next point, |
| 274 | // Ch is set to 0, and true is returned. This case is indistinguishable to |
| 275 | // the caller from a null byte being successfully converted to a null |
| 276 | // character, but a null character is never printable under any settings and |
| 277 | // that is how invalid bytes are treated as well. |
| 278 | // |
| 279 | // The number of skipped bytes for invalid characters follows the Unicode |
| 280 | // definition of the maximal subpart of an ill-formed subsequence, applied |
| 281 | // to arbitrary locales: it is the longest subsequence that could start a |
| 282 | // valid multibyte character, or if the initial byte cannot start a valid |
| 283 | // multibyte character, the initial byte. This allows consistent error |
| 284 | // recovery. |
| 285 | static bool readChar(const char *&Cur, const char *End, bool AtEOF, |
| 286 | CharT &Ch); |
| 287 | |
| 288 | static bool isStringChar(CharT Ch); |
| 289 | |
| 290 | static void startString() {} |
| 291 | |
| 292 | static void writeString(raw_ostream &OS, StringRef Str) { OS << Str; } |
| 293 | |
| 294 | static void endString(raw_ostream &OS) { OS << '\n'; } |
| 295 | }; |
| 296 | |
| 297 | struct MultiByteStateless : Base<UTF32> { |
| 298 | std::mbstate_t InMBState{}; |
| 299 | |
| 300 | bool readChar(const char *&Cur, const char *End, bool AtEOF, CharT &Ch) { |
| 301 | if (Cur == End) |
| 302 | return false; |
| 303 | |
| 304 | static const UTF32(&LookupTable)[0x100] = [&]() -> const UTF32(&)[0x100] { |
| 305 | static UTF32 LookupTable[0x100]; |
| 306 | |
| 307 | for (int I = 0; I < 0x100; ++I) { |
| 308 | const unsigned char Ch = I; |
| 309 | const char *Cur = reinterpret_cast<const char *>(&Ch); |
| 310 | char_t UCh = 0; |
| 311 | std::mbstate_t InitialMBState{}; |
| 312 | if (mbrtoc(Ch: &UCh, S: Cur, N: 1, MBState: &InitialMBState) == 1) |
| 313 | LookupTable[Ch] = UCh; |
| 314 | } |
| 315 | |
| 316 | return LookupTable; |
| 317 | }(); |
| 318 | |
| 319 | char_t UCh; |
| 320 | |
| 321 | if (std::mbsinit(ps: &InMBState)) { |
| 322 | UCh = LookupTable[static_cast<unsigned char>(*Cur)]; |
| 323 | if (UCh || !*Cur) { |
| 324 | ++Cur; |
| 325 | Ch = UCh; |
| 326 | return true; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | const char *Next = Cur; |
| 331 | std::mbstate_t NextMBState = InMBState; |
| 332 | for (;;) { |
| 333 | // Read one byte at a time. This is usually not the best way to use |
| 334 | // mbrtoc32(): usually passing the size of the buffer reduces the |
| 335 | // number of times it needs to be called. The strings utility is unusual |
| 336 | // in that it is expected to encounter many bytes that do not form valid |
| 337 | // characters and to recover from the subsequent decoding errors, |
| 338 | // however, so it is more useful to optimise for this case. |
| 339 | const size_t BytesRead = mbrtoc(Ch: &UCh, S: Next, N: 1, MBState: &NextMBState); |
| 340 | switch (BytesRead) { |
| 341 | case 1: |
| 342 | ++Next; |
| 343 | LLVM_FALLTHROUGH; |
| 344 | |
| 345 | case (size_t)-3: |
| 346 | Cur = Next; |
| 347 | InMBState = NextMBState; |
| 348 | Ch = std::min<UTF32>(a: UCh, b: 0x10FFFF); |
| 349 | return true; |
| 350 | |
| 351 | case (size_t)-2: |
| 352 | // We encountered a byte that is a valid start or continuation of a |
| 353 | // multibyte character. If we have more bytes, carry on. If we don't |
| 354 | // have more bytes yet, but we are not at the end of file, return |
| 355 | // false. If we don't have more bytes and we are at the end of file, |
| 356 | // fall through to treat it as an error. |
| 357 | ++Next; |
| 358 | if (Next != End) |
| 359 | continue; |
| 360 | if (!AtEOF) |
| 361 | return false; |
| 362 | LLVM_FALLTHROUGH; |
| 363 | |
| 364 | case 0: |
| 365 | case (size_t)-1: |
| 366 | // We cannot form a valid non-null character. |
| 367 | // |
| 368 | // If we processed any bytes already that formed an incomplete |
| 369 | // multibyte character, treat those bytes as a single null character, |
| 370 | // otherwise treat the current byte as a single null character. |
| 371 | if (Next == Cur) |
| 372 | ++Next; |
| 373 | Cur = Next; |
| 374 | InMBState = {}; |
| 375 | Ch = 0; |
| 376 | return true; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | static bool isStringChar(CharT Ch) { |
| 382 | using StringCharSet = std::bitset<0x110000>; |
| 383 | static const StringCharSet &StringChars = []() -> const StringCharSet & { |
| 384 | static StringCharSet StringChars; |
| 385 | |
| 386 | using llvm::sys::unicode::GeneratedCharacterRanges; |
| 387 | using llvm::sys::unicode::GeneratedControlCharacterRanges; |
| 388 | using llvm::sys::UnicodeCharRange; |
| 389 | |
| 390 | for (const UnicodeCharRange &Range : GeneratedCharacterRanges) |
| 391 | for (UTF32 Ch = Range.Lower; Ch <= Range.Upper; ++Ch) |
| 392 | StringChars[Ch] = true; |
| 393 | for (const UnicodeCharRange &Range : GeneratedControlCharacterRanges) |
| 394 | for (UTF32 Ch = Range.Lower; Ch <= Range.Upper; ++Ch) |
| 395 | StringChars[Ch] = false; |
| 396 | StringChars[U'\t'] = true; |
| 397 | |
| 398 | return StringChars; |
| 399 | }(); |
| 400 | |
| 401 | return StringChars[Ch]; |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | // Note: The complexity in writing is only required for stateful encodings |
| 406 | // such as the ISO-2022 ones. |
| 407 | struct MultiByteStateful : MultiByteStateless { |
| 408 | std::mbstate_t StrMBState{}, OutMBState{}; |
| 409 | |
| 410 | void startString() { StrMBState = InMBState; } |
| 411 | |
| 412 | void writeString(raw_ostream &OS, StringRef Str) { |
| 413 | const char *Cur = Str.begin(), *End = Str.end(); |
| 414 | while (Cur != End) { |
| 415 | char_t UCh; |
| 416 | const size_t BytesRead = mbrtoc(Ch: &UCh, S: Cur, N: End - Cur, MBState: &StrMBState); |
| 417 | assert(BytesRead != 0 && BytesRead != (size_t)-1 && |
| 418 | BytesRead != (size_t)-2 && |
| 419 | "If a string was initially valid, it cannot become invalid when " |
| 420 | "re-reading" ); |
| 421 | if (BytesRead != (size_t)-3) |
| 422 | Cur += BytesRead; |
| 423 | |
| 424 | char Buf[MB_LEN_MAX]; |
| 425 | const size_t BytesWritten = crtomb(S: Buf, Ch: UCh, MBState: &OutMBState); |
| 426 | assert(BytesWritten != (size_t)-1 && |
| 427 | "If we successfully read a character, we must also be able to " |
| 428 | "write it" ); |
| 429 | OS << StringRef(Buf, BytesWritten); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void endString(raw_ostream &OS) { |
| 434 | char Buf[MB_LEN_MAX]; |
| 435 | const size_t BytesWritten = crtomb(S: Buf, Ch: 0, MBState: &OutMBState); |
| 436 | assert(BytesWritten != (size_t)-1 && |
| 437 | "A null character must always be valid" ); |
| 438 | Buf[BytesWritten - 1] = '\n'; |
| 439 | OS << StringRef(Buf, BytesWritten); |
| 440 | } |
| 441 | }; |
| 442 | |
| 443 | struct Utf8 : MultiByteStateless { |
| 444 | static bool readChar(const char *&Cur, const char *End, bool AtEOF, |
| 445 | CharT &Ch) { |
| 446 | if (Cur == End) |
| 447 | return false; |
| 448 | |
| 449 | if (static_cast<unsigned char>(*Cur) <= 0x7F) { |
| 450 | Ch = *Cur++; |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | const UTF8 *UTF8Cur = reinterpret_cast<const UTF8 *>(Cur); |
| 455 | const UTF8 *UTF8Next = UTF8Cur; |
| 456 | const UTF8 *UTF8End = reinterpret_cast<const UTF8 *>(End); |
| 457 | UTF32 *UTF32Next = &Ch; |
| 458 | const auto Res = ConvertUTF8toUTF32(sourceStart: &UTF8Next, sourceEnd: UTF8End, targetStart: &UTF32Next, |
| 459 | targetEnd: &Ch + 1, flags: strictConversion); |
| 460 | if (UTF8Next != UTF8Cur) { |
| 461 | assert(UTF32Next != &Ch); |
| 462 | assert(Ch <= 0x10FFFF && |
| 463 | "ConvertUTF8toUTF32 produced an out-of-range character" ); |
| 464 | } else if (Res == sourceExhausted && !AtEOF) { |
| 465 | return false; |
| 466 | } else { |
| 467 | assert(UTF32Next == &Ch); |
| 468 | UTF8Next += |
| 469 | findMaximalSubpartOfIllFormedUTF8Sequence(source: UTF8Next, sourceEnd: UTF8End); |
| 470 | Ch = 0; |
| 471 | } |
| 472 | Cur = reinterpret_cast<const char *>(UTF8Next); |
| 473 | return true; |
| 474 | } |
| 475 | }; |
| 476 | |
| 477 | struct SingleByte : Base<unsigned char> { |
| 478 | static bool readChar(const char *&Cur, const char *End, bool AtEOF, |
| 479 | CharT &Ch) { |
| 480 | if (Cur == End) |
| 481 | return false; |
| 482 | |
| 483 | Ch = *reinterpret_cast<const unsigned char *>(Cur++); |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | static bool isStringChar(CharT Ch) { |
| 488 | using StringCharSet = std::bitset<0x100>; |
| 489 | static const StringCharSet &StringChars = []() -> const StringCharSet & { |
| 490 | static StringCharSet StringChars; |
| 491 | |
| 492 | MultiByteStateless MB; |
| 493 | for (int I = 0; I < 0x100; ++I) { |
| 494 | const unsigned char Ch = I; |
| 495 | const char *Cur = reinterpret_cast<const char *>(&Ch), *End = Cur + 1; |
| 496 | MultiByteStateless::CharT UCh; |
| 497 | if (MB.readChar(Cur, End, /*AtEOF=*/true, Ch&: UCh) && |
| 498 | MB.isStringChar(Ch: UCh)) |
| 499 | StringChars[I] = true; |
| 500 | } |
| 501 | |
| 502 | return StringChars; |
| 503 | }(); |
| 504 | |
| 505 | return StringChars[Ch]; |
| 506 | } |
| 507 | }; |
| 508 | |
| 509 | struct Ascii : SingleByte { |
| 510 | static bool isStringChar(CharT Ch) { |
| 511 | return Ch == '\t' || (Ch >= ' ' && Ch <= '~'); |
| 512 | } |
| 513 | }; |
| 514 | }; |
| 515 | |
| 516 | template <typename CharSetT> |
| 517 | void strings(raw_ostream &OS, StringRef FileName, sys::fs::file_t Handle) { |
| 518 | using CharT = typename CharSetT::CharT; |
| 519 | CharSetT CharSet; |
| 520 | |
| 521 | SmallString<sys::fs::DefaultReadChunkSize> Buffer; |
| 522 | auto = [&OS, FileName](size_t StringStart) { |
| 523 | if (PrintFileName) |
| 524 | OS << FileName << ": " ; |
| 525 | switch (Radix) { |
| 526 | case Radix::None: |
| 527 | break; |
| 528 | case Radix::Octal: |
| 529 | OS << format(Fmt: "%7zo " , Vals: StringStart); |
| 530 | break; |
| 531 | case Radix::Hexadecimal: |
| 532 | OS << format(Fmt: "%7zx " , Vals: StringStart); |
| 533 | break; |
| 534 | case Radix::Decimal: |
| 535 | OS << format(Fmt: "%7zu " , Vals: StringStart); |
| 536 | break; |
| 537 | } |
| 538 | }; |
| 539 | |
| 540 | // To handle very large files without consuming excessive memory, we read the |
| 541 | // file in a little at a time and process it then rather than reading the |
| 542 | // entire file at once. |
| 543 | // |
| 544 | // A string is only buffered until it is known to be long enough to print; |
| 545 | // from then on it is streamed out directly, so an arbitrarily long string |
| 546 | // never needs an arbitrarily large buffer. Candidate therefore only ever |
| 547 | // holds a run that is shorter than MinLength and that was cut off by the end |
| 548 | // of a chunk. |
| 549 | const size_t MinLengthChars = MinLength; |
| 550 | SmallString<DefaultMinLength> Candidate; |
| 551 | size_t CandidateLengthChars = 0; |
| 552 | bool InString = false; |
| 553 | // Offset of the start of the current chunk within the file. |
| 554 | size_t ChunkOffset = 0; |
| 555 | |
| 556 | while (true) { |
| 557 | // Size of any partial character left over from the previous chunk. |
| 558 | const size_t PendingCharSizeBytes = Buffer.size(); |
| 559 | Buffer.resize_for_overwrite(N: PendingCharSizeBytes + |
| 560 | sys::fs::DefaultReadChunkSize); |
| 561 | |
| 562 | Expected<size_t> ReadBytesOrErr = sys::fs::readNativeFile( |
| 563 | FileHandle: Handle, |
| 564 | Buf: MutableArrayRef(Buffer.begin() + PendingCharSizeBytes, Buffer.end())); |
| 565 | if (!ReadBytesOrErr) { |
| 566 | errs() << FileName << ": " |
| 567 | << errorToErrorCode(Err: ReadBytesOrErr.takeError()).message() << '\n'; |
| 568 | return; |
| 569 | } |
| 570 | const bool AtEOF = *ReadBytesOrErr == 0; |
| 571 | const size_t ChunkSize = PendingCharSizeBytes + *ReadBytesOrErr; |
| 572 | if (ChunkSize == 0) |
| 573 | break; |
| 574 | |
| 575 | Buffer.resize_for_overwrite(N: ChunkSize); |
| 576 | |
| 577 | // To prevent performance regression under O0, access the raw pointer |
| 578 | // instead of using methods provided by the standard library, which are not |
| 579 | // inlined under O0. |
| 580 | const char *Begin = Buffer.data(); |
| 581 | const char *End = Begin + ChunkSize; |
| 582 | const char *Cur = Begin; |
| 583 | |
| 584 | CharT Ch; |
| 585 | |
| 586 | // Handle the remaining part from the previous chunk. |
| 587 | // The previous chunk can be either shorter than MinSize or part of the |
| 588 | // string. |
| 589 | // Keep the buffer size bounded. With a small Min, a long string spanning |
| 590 | // multiple chunks will have at most DefaultReadChunkSize bytes, since the |
| 591 | // buffer is printed immediately with the header (guarded by the second if). |
| 592 | // With a large Min, the buffer must hold at least Min bytes, since we need |
| 593 | // enough data to decide whether to print it. |
| 594 | if (InString || !Candidate.empty()) { |
| 595 | const char *StringEnd; |
| 596 | size_t LengthChars = 0; |
| 597 | bool EndOfChunk; |
| 598 | |
| 599 | // Find the end of the current string. |
| 600 | for (;;) { |
| 601 | StringEnd = Cur; |
| 602 | EndOfChunk = !CharSet.readChar(Cur, End, AtEOF, Ch); |
| 603 | if (EndOfChunk || !CharSet.isStringChar(Ch)) |
| 604 | break; |
| 605 | ++LengthChars; |
| 606 | } |
| 607 | |
| 608 | size_t SizeBytes = StringEnd - Begin; |
| 609 | if (InString) { |
| 610 | // Print the remaining part if the previous chunk has already printed |
| 611 | // the header. E.g. header: aaaaa | bbbbb, where | is the chunk |
| 612 | // boundary. |
| 613 | // Output: Header: aaaaabbbbb, where bbbbb is printed in here. |
| 614 | CharSet.writeString(OS, StringRef(Begin, SizeBytes)); |
| 615 | } else if (CandidateLengthChars + LengthChars >= MinLengthChars) { |
| 616 | // If the header hasn't been printed yet (i.e. the previous candidate |
| 617 | // was smaller than Min), but we can print it now, print the header |
| 618 | // first, followed by the candidate from the previous chunk and the |
| 619 | // current string. E.g. aa | bbbbbb |
| 620 | // Output Header: aabbbbbb, where aabbbbbb is printed in here. |
| 621 | PrintHeader(ChunkOffset - Candidate.size()); |
| 622 | CharSet.writeString(OS, Candidate); |
| 623 | CharSet.writeString(OS, StringRef(Begin, SizeBytes)); |
| 624 | Candidate.clear(); |
| 625 | CandidateLengthChars = 0; |
| 626 | InString = true; |
| 627 | } else if (EndOfChunk) { |
| 628 | // If the current chunk + previous candidate is still smaller than Min, |
| 629 | // append it to Candidate. |
| 630 | Candidate.append(in_start: Begin, in_end: StringEnd); |
| 631 | CandidateLengthChars += LengthChars; |
| 632 | } else { |
| 633 | // If the string has terminated but is still smaller than Min, clear the |
| 634 | // buffer since it is too short to print. |
| 635 | Candidate.clear(); |
| 636 | CandidateLengthChars = 0; |
| 637 | } |
| 638 | |
| 639 | if (EndOfChunk) { |
| 640 | if (AtEOF) |
| 641 | break; |
| 642 | |
| 643 | // Finish handling the current chunk and update ChunkOffset. |
| 644 | ChunkOffset += Cur - Begin; |
| 645 | Buffer.erase(CS: Buffer.begin(), CE: Cur); |
| 646 | continue; |
| 647 | } |
| 648 | |
| 649 | if (InString) { |
| 650 | // We haven't reached the end of the chunk, which means the string is |
| 651 | // terminated. |
| 652 | CharSet.endString(OS); |
| 653 | InString = false; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // At this point, we are always at the start of a new string because the |
| 658 | // remaining part of the previous string has already been handled. |
| 659 | const char *StrHead = nullptr; |
| 660 | size_t LengthChars = 0; |
| 661 | for (;;) { |
| 662 | const char *Prev = Cur; |
| 663 | if (!StrHead) |
| 664 | CharSet.startString(); |
| 665 | if (!CharSet.readChar(Cur, End, AtEOF, Ch)) |
| 666 | break; |
| 667 | if (CharSet.isStringChar(Ch)) { |
| 668 | // Find the start of the next string. |
| 669 | if (!StrHead) |
| 670 | StrHead = Prev; |
| 671 | ++LengthChars; |
| 672 | } else if (StrHead) { |
| 673 | // If it is not a printable character, we have reached the end of the |
| 674 | // current string. Print it if long enough. |
| 675 | if (LengthChars >= MinLengthChars) { |
| 676 | PrintHeader(ChunkOffset + (StrHead - Begin)); |
| 677 | CharSet.writeString(OS, StringRef(StrHead, Prev - StrHead)); |
| 678 | CharSet.endString(OS); |
| 679 | } |
| 680 | StrHead = nullptr; |
| 681 | LengthChars = 0; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // The last string could span multiple chunks. If it is larger than Min, |
| 686 | // print the header immediately and set the InString flag to avoid printing |
| 687 | // it again. |
| 688 | if (StrHead) { |
| 689 | // Print it, or append it to Candidate if it is too short. |
| 690 | if (LengthChars >= MinLengthChars) { |
| 691 | PrintHeader(ChunkOffset + (StrHead - Begin)); |
| 692 | CharSet.writeString(OS, StringRef(StrHead, Cur - StrHead)); |
| 693 | InString = true; |
| 694 | } else { |
| 695 | Candidate.append(in_start: StrHead, in_end: Cur); |
| 696 | CandidateLengthChars += LengthChars; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | ChunkOffset += Cur - Begin; |
| 701 | Buffer.erase(CS: Buffer.begin(), CE: Cur); |
| 702 | |
| 703 | if (AtEOF) |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | if (InString) |
| 708 | CharSet.endString(OS); |
| 709 | } |
| 710 | } // namespace |
| 711 | |
| 712 | int main(int argc, char **argv) { |
| 713 | std::setlocale(LC_ALL, locale: "" ); |
| 714 | |
| 715 | InitLLVM X(argc, argv); |
| 716 | BumpPtrAllocator A; |
| 717 | StringSaver Saver(A); |
| 718 | StringsOptTable Tbl; |
| 719 | ToolName = argv[0]; |
| 720 | opt::InputArgList Args = |
| 721 | Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, |
| 722 | ErrorFn: [&](StringRef Msg) { reportCmdLineError(Message: Msg); }); |
| 723 | if (Args.hasArg(Ids: OPT_help)) { |
| 724 | Tbl.printHelp( |
| 725 | OS&: outs(), |
| 726 | Usage: (Twine(ToolName) + " [options] <input object files>" ).str().c_str(), |
| 727 | Title: "llvm string dumper" ); |
| 728 | // TODO Replace this with OptTable API once it adds extrahelp support. |
| 729 | outs() << "\nPass @FILE as argument to read options from FILE.\n" ; |
| 730 | return 0; |
| 731 | } |
| 732 | if (Args.hasArg(Ids: OPT_version)) { |
| 733 | outs() << ToolName << '\n'; |
| 734 | cl::PrintVersionMessage(); |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | Arg *EncodingArg = Args.getLastArg(Ids: OPT_encoding_EQ); |
| 739 | if (!EncodingArg) { |
| 740 | Encoding = Encoding::Locale; |
| 741 | } else { |
| 742 | auto EncodingVal = llvm::StringSwitch<std::optional<enum Encoding>>( |
| 743 | EncodingArg->getValue()) |
| 744 | .Case(S: "s" , Value: Encoding::Ascii) |
| 745 | .Case(S: "S" , Value: Encoding::Locale) |
| 746 | .Case(S: "utf8" , Value: Encoding::Utf8) |
| 747 | .Default(Value: std::nullopt); |
| 748 | if (!EncodingVal) |
| 749 | invalidArgValue(Arg: EncodingArg); |
| 750 | Encoding = *EncodingVal; |
| 751 | } |
| 752 | parseIntArg(Args, ID: OPT_bytes_EQ, Value&: MinLength); |
| 753 | PrintFileName = Args.hasArg(Ids: OPT_print_file_name); |
| 754 | Arg *RadixArg = Args.getLastArg(Ids: OPT_radix_EQ); |
| 755 | if (!RadixArg) { |
| 756 | Radix = Radix::None; |
| 757 | } else { |
| 758 | Radix = llvm::StringSwitch<enum Radix>(RadixArg->getValue()) |
| 759 | .Case(S: "o" , Value: Radix::Octal) |
| 760 | .Case(S: "d" , Value: Radix::Decimal) |
| 761 | .Case(S: "x" , Value: Radix::Hexadecimal) |
| 762 | .Default(Value: Radix::None); |
| 763 | if (Radix == Radix::None) |
| 764 | invalidArgValue(Arg: RadixArg); |
| 765 | } |
| 766 | |
| 767 | if (MinLength == 0) { |
| 768 | errs() << "invalid minimum string length 0\n" ; |
| 769 | return EXIT_FAILURE; |
| 770 | } |
| 771 | |
| 772 | void (*const StringsImpl)(raw_ostream &OS, StringRef FileName, |
| 773 | sys::fs::file_t Handle) = [] { |
| 774 | switch (Encoding) { |
| 775 | case Encoding::Ascii: |
| 776 | return strings<CharSet::Ascii>; |
| 777 | |
| 778 | case Encoding::Locale: |
| 779 | if (MB_CUR_MAX == 1) |
| 780 | return strings<CharSet::SingleByte>; |
| 781 | |
| 782 | #ifdef LLVM_ON_UNIX |
| 783 | // Using LLVM's built-in decoder is faster than going through libc, but |
| 784 | // this doesn't affect correctness (assuming 32-bit wchar_t), so it's fine |
| 785 | // if this check has false negatives (e.g. if some OS returns "utf8" |
| 786 | // instead). |
| 787 | if (StringRef(nl_langinfo(CODESET)) == "UTF-8" ) |
| 788 | return strings<CharSet::Utf8>; |
| 789 | #endif |
| 790 | #ifdef _WIN32 |
| 791 | // Using LLVM's built-in decoder is necessary for correctness as |
| 792 | // Strings<CharSet::MultiByteStateless> on Windows is implemented on top |
| 793 | // of 16-bit wchar_t which cannot represent all code points. |
| 794 | if (GetACP() == CP_UTF8) |
| 795 | return strings<CharSet::Utf8>; |
| 796 | #endif |
| 797 | |
| 798 | if (!mblen(NULL, n: 0)) |
| 799 | return strings<CharSet::MultiByteStateless>; |
| 800 | else |
| 801 | return strings<CharSet::MultiByteStateful>; |
| 802 | |
| 803 | case Encoding::Utf8: |
| 804 | return strings<CharSet::Utf8>; |
| 805 | } |
| 806 | |
| 807 | llvm_unreachable("unhandled encoding" ); |
| 808 | }(); |
| 809 | |
| 810 | std::vector<std::string> InputFileNames = Args.getAllArgValues(Id: OPT_INPUT); |
| 811 | if (InputFileNames.empty()) |
| 812 | InputFileNames.push_back(x: "-" ); |
| 813 | |
| 814 | for (const auto &File : InputFileNames) { |
| 815 | if (File == "-" ) { |
| 816 | StringsImpl(llvm::outs(), "{standard input}" , sys::fs::getStdinHandle()); |
| 817 | } else { |
| 818 | Expected<sys::fs::file_t> FDOrErr = |
| 819 | sys::fs::openNativeFileForRead(Name: File, Flags: sys::fs::OF_TextWithCRLF); |
| 820 | if (!FDOrErr) { |
| 821 | errs() << File |
| 822 | << ": cannot open file: " << toString(E: FDOrErr.takeError()) |
| 823 | << '\n'; |
| 824 | continue; |
| 825 | } |
| 826 | StringsImpl(llvm::outs(), File, *FDOrErr); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return EXIT_SUCCESS; |
| 831 | } |
| 832 | |