Currently, I'm writing an LLVM bitcode parser just for fun, however, I've run into an issue where the parser just throws an error because it reads wrong values from the file. I think I understood the way the file is structured and if I try and parse a simple program the output of my parser looks like this:
/*
Attributes are written in brackets, a colon is followed by a value.
The first Block attribute is its kind, the second one is its length, and the third one is its layer. The reason that a layer 1 is printed within the same level as a
level 0 here, is that every block get's printed as soon as it has been parsed, so the
BlockInfo at layer one is part of a block that fails to parse.
*/
Block(Identification,160,0):
├── Abbreviation:
│ ├── Literal: 1
│ ├── Array(Char)
│ └── Char
├── Abbreviation:
│ ├── Literal: 1
│ ├── Array(Char)
│ │ ├── Char: L
│ │ ├── Char: L
│ │ ├── Char: V
│ │ ├── Char: M
│ │ ├── Char: 1
│ │ ├── Char: 2
│ │ ├── Char: .
│ │ ├── Char: 0
│ │ ├── Char: .
│ │ └── Char: 1
│ └── Char
├── Abbreviation:
│ ├── Literal: 2
│ └── VBR(6)
└── Abbreviation:
├── Literal: 2
└── VBR(6): 0
Block(BlockInfo,704,1):
├── Record(1):
│ └── 14
├── Abbreviation:
│ ├── Fixed(3)
│ ├── VBR(8)
│ ├── Array(Fixed)
│ └── Fixed(8)
├── Abbreviation:
│ ├── Literal: 1
│ ├── VBR(8)
│ ├── Array(Fixed)
│ └── Fixed(7)
├── Abbreviation:
│ ├── Literal: 1
│ ├── VBR(8)
│ ├── Array(Char)
│ └── Char
├── Abbreviation:
│ ├── Literal: 2
│ ├── VBR(8)
│ ├── Array(Char)
│ └── Char
├── Record(1):
│ └── 11
├── Abbreviation:
│ ├── Literal: 1
│ └── Fixed(4)
├── Abbreviation:
│ ├── Literal: 4
│ └── VBR(8)
├── Abbreviation:
│ ├── Literal: 11
│ ├── Fixed(4)
│ ├── Fixed(4)
│ └── VBR(8)
├── Abbreviation:
│ └── Literal: 2
├── Record(1):
│ └── 12
├── Abbreviation:
│ ├── Literal: 20
│ ├── VBR(6)
│ ├── Fixed(4)
│ ├── VBR(4)
│ └── Fixed(1)
├── Abbreviation:
│ ├── Literal: 56
│ ├── VBR(6)
│ └── Fixed(4)
├── Abbreviation:
│ ├── Literal: 56
│ ├── VBR(6)
│ ├── Fixed(4)
│ └── Fixed(8)
├── Abbreviation:
│ ├── Literal: 2
│ ├── VBR(6)
│ ├── VBR(6)
│ └── Fixed(4)
├── Abbreviation:
│ ├── Literal: 2
│ ├── VBR(6)
│ ├── VBR(6)
│ ├── Fixed(4)
│ └── Fixed(8)
├── Abbreviation:
│ ├── Literal: 3
│ ├── VBR(6)
│ ├── Fixed(4)
│ └── Fixed(4)
├── Abbreviation:
│ └── Literal: 10
├── Abbreviation:
│ ├── Literal: 10
│ └── VBR(6)
├── Abbreviation:
│ └── Literal: 15
└── Abbreviation:
├── Literal: 43
├── Fixed(1)
├── Fixed(4)
├── Array(VBR)
└── VBR(6)
Block(Unknown(2),198944,2):
As you can see, the last block it is trying to parse has the ID 2 and fails to read its correct size. Does Block id 2 has to be parsed in a different way than other Blocks?