1

I have two questions:

Is it possible to add custom DWARF on the resulting binary of a C program? (I explain later why i want to do this)

How does DWARF work?

First of all, i don't understand DWARF. I tried to read some docs on dwarfstd.org, but i think it's to high for me. Maybe someone could give me some basic instructions which helps me to dig deeper (the entry point is a bit difficult for me).

Why i want to do this? I like playing around with writing my own compiler, implementing my own language. My goal is to write a compiled language and not an interpreted or jitted one. So i have several options as a backend: C, Opcodes, ASM, LLVM and maybe there are a lot more. Because LLVM is a C++ library (and i have no clue about C++) i tried it a little bit using the C wrapper. Since i'm a newbie on C too i didn't got it working easily (but i didn't investigate a lot). The problem with Opcodes and ASM is, that the learning curve is higher than LLVM and i'm even more than a newbie on that topic. So, i would like to use C as a backend... but i think about some problems: Debugging info. The resulting C file would have different function names than my source language and even different line numbers. I know that line numbers could be fixed using the #line directive in C but it's not 100% perfect, though. So i'm looking for a really good solution for this before i start implementing something odd. I stumbled upon DWARF and the i got those question.

If anyone knows a well documented alternative to LLVM which would fit my requirements, your welcome to tell me :)

My requirements for target platform are at least: x86, x64 and ARM

Daniel Däschle
  • 766
  • 10
  • 32
  • 1
    Yes, it is possible to add custom DWARF, but I think that you should perhaps decide what you want to do. I suggest that you ignore DWARF until you're able to generate a hello-world program, or perhaps fizzbuzz. By then, parts of DWARF will seem familiar, because they resemble things you have in your code, and until then, the DWARF spec is a bit of a wall. I'll vote to close the question now, as it isn't about any one subject. – arnt Aug 05 '20 at 06:46
  • An unrelated comment. Someone who wants to implement a language should be open to learning others' languages, even one as ugly as C++. Learn BCPL, C++, Scala, awk, Lua, Oberon, ML, Forth. Learning their different viewpoints will help you create a better language... and will make it simple to use LLVM the way it's meant to be used, too. Open source is only really open if you're able to read it. – arnt Aug 05 '20 at 06:57
  • 1
    You could try to start compiling with `-S -g3` and see the assembly output+ debug infos. – JCWasmx86 Aug 05 '20 at 07:21
  • @arnt The question is: How is it possible? Would be nice if you could answer that to me :) Thanks for all. – Daniel Däschle Aug 05 '20 at 20:21
  • It depends on what custom things you want. LLVM already contains support for writing most of what debuggers read, using functons and types whose name start with DI, e.g. DICompileUnit, DIBasicType. DICompositeType, DIFlags... generally one makes a data structure of objects that reference each other, so a variable's metadata refers to a DIType, etc. When you want something really special my answer would probably be something along the lines of "subclass this LLVM class and patch that function". But you haven't revealed what the qustion is, merely that you have one and want an answer. – arnt Aug 06 '20 at 06:40
  • I don't want to use LLVM. I want to either use C as a backend (and then i don't know how to add good debugging information) or there is a good alternative to LLVM. – Daniel Däschle Aug 07 '20 at 07:13

0 Answers0