[llvm] New tool 'llvm-elf2bin'. (NOT READY FOR REVIEW – NO TESTS) (PR #73625)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 28 08:18:16 PST 2023


================
@@ -0,0 +1,199 @@
+//===- hex.cpp - Code to write Intel and Motorola Hex for llvm-elf2bin ----===//
+//
+// 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-elf2bin.h"
+
+#include <assert.h>
+
+#include "llvm/Support/Endian.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+
+class Hex {
+public:
+  virtual void data(uint64_t addr, const std::string &data) = 0;
+  virtual void trailer(uint64_t entry) = 0;
+  virtual ~Hex() = default;
+};
+
+template <typename Integer> std::string bigend(Integer i, size_t bytes_wanted) {
+  assert(bytes_wanted <= sizeof(i));
+  char buf[sizeof(i)];
+  llvm::support::endian::write(buf, i, llvm::endianness::big);
+  return std::string(buf + sizeof(i) - bytes_wanted, bytes_wanted);
+}
+
+class IHex : public Hex {
+  static std::string record(uint8_t type, uint16_t addr,
+                            const std::string &data) {
+    std::string binstring;
+    llvm::raw_string_ostream binstream(binstring);
+
+    binstream << (char)data.size() << bigend(addr, 2) << (char)type << data;
+
+    uint8_t checksum = 0;
+    for (uint8_t c : binstring)
+      checksum -= c;
+    binstream << (char)checksum;
+
+    std::string hexstring;
+    llvm::raw_string_ostream hexstream(hexstring);
+
+    hexstream << ':';
+    for (uint8_t c : binstring) {
+      static const char hexdigits[] = "0123456789ABCDEF";
+      hexstream << hexdigits[c >> 4] << hexdigits[c & 0xF];
----------------
MaskRay wrote:

llvm::hexdigit from StringExtras.h

https://github.com/llvm/llvm-project/pull/73625


More information about the llvm-commits mailing list