[llvm] [llvm-objcopy] Support SREC output format (PR #75874)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 19 01:06:01 PST 2024


================
@@ -390,6 +392,104 @@ class IHexWriter : public Writer {
   IHexWriter(Object &Obj, raw_ostream &Out) : Writer(Obj, Out) {}
 };
 
+using SRecLineData = SmallVector<char, 64>;
+struct SRecord {
+  uint8_t Type;
+  uint32_t Address;
+  ArrayRef<uint8_t> Data;
+  SRecLineData toString() const;
+  uint8_t getCount() const;
+  // get address size in characters
+  uint8_t getAddressSize() const;
+  uint8_t getChecksum() const;
+  size_t getSize() const;
+  static SRecord getHeader(StringRef FileName);
+  static uint8_t getType(uint32_t Address);
+  enum Type : uint8_t {
+    // Vendor specific text comment
+    S0 = 0,
+    // Data that starts at a 16 bit address
+    S1 = 1,
+    // Data that starts at a 24 bit address
+    S2 = 2,
+    // Data that starts at a 32 bit address
+    S3 = 3,
+    // Reserved
+    S4 = 4,
+    // 16 bit count of S1/S2/S3 records (optional)
+    S5 = 5,
+    // 32 bit count of S1/S2/S3 records (optional)
+    S6 = 6,
+    // Terminates a series of S3 records
+    S7 = 7,
+    // Terminates a series of S2 records
+    S8 = 8,
+    // Terminates a series of S1 records
+    S9 = 9
+  };
+};
+
+/// The real writer
+class SRECWriter : public Writer {
+  StringRef OutputFileName;
+  size_t TotalSize = 0;
+  std::vector<const SectionBase *> Sections;
+
+  size_t writeHeader(uint8_t *Buf);
+  size_t writeTerminator(uint8_t *Buf, uint8_t Type);
+  Error checkSection(const SectionBase &S) const;
+
+public:
+  ~SRECWriter() = default;
+  Error finalize() override;
+  Error write() override;
+  SRECWriter(Object &Obj, raw_ostream &OS, StringRef OutputFile)
+      : Writer(Obj, OS), OutputFileName(OutputFile) {}
+};
+
+/// Base class for SRecSectionWriter
+/// This class does not actually write anything. It is only used for size
+/// calculation.
+class SRECSectionWriterBase : public BinarySectionWriter {
----------------
jh7370 wrote:

This doesn't look or sound like it's describing an "is a" relationship to me, which is what inheritance should be used for. It sounds more like you've got two separate classes which share some common behaviour, which would mean you should have a common abstract base class that they both share, with two concrete sub-classes (or two independent classes that use a third class, but don't inherit from it).

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


More information about the llvm-commits mailing list