1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
use std::{error::Error, hash::Hash, hash::Hasher};
use std::collections::{HashMap, hash_map::DefaultHasher};

use crate::{KOFileReader, KOFileWriter, SectionHeader, RelInstruction};

/// Represents a list of strings in a KO file that can be used as the names of
/// symbols or as file comments or notes
pub struct StringTable {
    strings: Vec<String>,
    name: String,
    size: u32,
}

impl StringTable {

    /// Creates a new string table with the specified name
    pub fn new(name: &str) -> StringTable {
        StringTable {
            strings: vec![String::new()],
            name: name.to_owned(),
            size: 1,
        }
    }

    /// Reads the string table from the KO file reader, and returns it
    pub fn read(reader: &mut KOFileReader, header: &SectionHeader) -> Result<StringTable, Box<dyn Error>> {

        // Consume that first \0, because the string table is intialized to a length of 1 already
        reader.next()?;

        let mut strtab = StringTable::new(header.name());

        while strtab.size() < header.section_size() {
            strtab.add_no_check(&reader.read_string()?);
        }

        if strtab.size() > header.section_size() {
            return Err("Error reading string table, size mismatch".into());
        }

        Ok(strtab)

    }

    /// Writes the entire string table to the KO file
    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        for s in self.strings.iter() {
            writer.write_string(s)?;
        }

        Ok(())
    }

    /// Returns the size of this string table in bytes
    pub fn size(&self) -> u32 {
        self.size
    }

    /// Adds a string to the string table
    /// This function checks first to see if the string already exists
    /// in the string table, which could possibly save space. If it does,
    /// it returns the index of that string. If it doesn't find it, the
    /// string is added to the string table.
    pub fn add(&mut self, s: &str) -> usize {

        let strval = s.to_owned();

        // Check if this string already exists
        if self.strings.contains(&strval) {
            match self.strings.binary_search(&strval) {
                Ok(idx) => { return idx; },
                _ => unreachable!()
            }
        }

        // Add the string to the table
        self.strings.push(strval);

        // Calculate the new size of the string table
        self.size += s.len() as u32 + 1;

        self.strings.len() - 1
    }

    /// Adds a string to the string table unconditionally
    /// Returns the index of the string in the string table
    /// See add()
    pub fn add_no_check(&mut self, s: &str) -> usize {

        // Add the string to the table
        self.strings.push(s.to_owned());

        // Calculate the new size of the string table
        self.size += s.len() as u32 + 1;

        self.strings.len() - 1
    }

    /// Returns the specific string at an index into the internal vector of strings
    pub fn get(&self, index: usize) -> Result<&String, Box<dyn Error>>{
        match self.strings.get(index) {
            Some(s) => Ok(s),
            None => Err(format!("Could not find string at index {} in string table.", index).into()),
        }
    }

    /// Returns the name of this string table
    pub fn name(&self) -> &String {
        &self.name
    }

    /// Returns a reference to the internal vector containing all strings in this string table
    pub fn get_strings(&self) -> &Vec<String> {
        &self.strings
    }

}

pub struct SymbolTable {
    symbols: Vec<Symbol>,
    name: String,
    size: u32,
    hash_to_index: HashMap<u64, usize>,
}

impl SymbolTable {

    pub fn new(name: &str) -> SymbolTable {
        SymbolTable {
            symbols: Vec::new(),
            name: name.to_owned(),
            size: 0,
            hash_to_index: HashMap::new(),
        }
    }

    pub fn read(reader: &mut KOFileReader, header: &SectionHeader, symstrtab: &StringTable, symdata: &SymbolDataSection) -> Result<SymbolTable, Box<dyn Error>> {

        let mut symtab = SymbolTable::new(header.name());

        while symtab.size() < header.section_size() {
            symtab.add_no_check(Symbol::read(symstrtab, symdata, reader)?);
        }

        if symtab.size() > header.section_size() {
            return Err("Error reading symbol table, size mismatch".into());
        }

        Ok(symtab)

    }

    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        for s in self.symbols.iter() {
            s.write(writer)?;
        }

        Ok(())
    }

    pub fn size(&self) -> u32 {
        self.size
    }

    pub fn add(&mut self, s: Symbol) -> usize {

        let mut hasher = DefaultHasher::new();
        s.hash(&mut hasher);
        let s_hash = hasher.finish();

        let s_index = self.symbols.len();

        match self.hash_to_index.get(&s_hash) {
            Some(index) => { return *index; },
            None => { self.hash_to_index.insert(s_hash, s_index); }
        }

        self.symbols.push(s);

        self.size += self.symbols.last().unwrap().width();

        s_index
    }

    pub fn add_no_check(&mut self, s: Symbol) -> usize {
        self.symbols.push(s);

        self.size += self.symbols.last().unwrap().width();

        self.symbols.len() - 1
    }

    pub fn get(&self, index: usize) -> Result<&Symbol, Box<dyn Error>>{
        match self.symbols.get(index) {
            Some(s) => Ok(s),
            None => Err(format!("Could not find symbol at index {} in symbol table.", index).into()),
        }
    }

    pub fn get_symbols(&self) -> &Vec<Symbol> {
        &self.symbols
    }

    pub fn name(&self) -> &String {
        &self.name
    }
}

/// Represents a single symbol in an object file.
/// This can be a function, an object, program data, or anything else.
#[derive(Debug, Clone)]
pub struct Symbol {
    name_index: usize,
    value_index: usize,
    name: String,
    value: KOSValue,
    size: u16,
    info: SymbolInfo,
    symbol_type: SymbolType,
    section_index: usize,
}

impl Hash for Symbol {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.to_string().hash(state);
    }
}

impl Symbol {

    /// Creates a new symbol from parts
    pub fn new(name: &str, value: KOSValue, size: u16, info: SymbolInfo, symbol_type: SymbolType, section_index: usize) -> Symbol {
        Symbol {
            name_index: 0,
            value_index: 0,
            name: name.to_owned(),
            value,
            size,
            info,
            symbol_type,
            section_index
        }
    }

    /// Reads a symbol from the symbol table of a KO file
    pub fn read(symstrtab: &StringTable, symdata: &SymbolDataSection, reader: &mut KOFileReader) -> Result<Symbol, Box<dyn Error>> {
        let name_index = reader.read_uint32()? as usize;
        let value_index = reader.read_uint32()? as usize;
        let size = reader.read_uint16()?;
        let info = SymbolInfo::from(reader.next()?)?;
        let symbol_type = SymbolType::from(reader.next()?)?;
        let section_index = reader.read_uint16()? as usize;

        Ok(Symbol {
            name_index,
            value_index,
            name: symstrtab.get(name_index)?.to_owned(),
            value: symdata.get(value_index)?.to_owned(),
            size,
            info,
            symbol_type,
            section_index,
        })
    }

    /// Write this symbol to a KOFileWriter
    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        writer.write_uint32(self.name_index as u32)?;

        writer.write_uint32(self.value_index as u32)?;

        writer.write_uint16(self.size)?;

        writer.write(self.info.to_byte())?;

        writer.write(self.symbol_type.to_byte())?;

        writer.write_uint16(self.section_index as u16)?;

        Ok(())
    }

    /// Returns the size of the data this symbol represents as stored in the KO file
    pub fn size(&self) -> u16 {
        self.size
    }

    /// Returns the width of one symbol in a symbol table
    pub fn width(&self) -> u32 {
        // Symbol table entries are always 14 bytes wide
        14
    }

    /// Returns the stored name of this symbol
    pub fn name(&self) -> &String {
        &self.name
    }

    /// Returns the kOS value that this symbol is associated with
    pub fn value(&self) -> &KOSValue {
        &self.value
    }

    /// Returns this symbol's type
    pub fn get_type(&self) -> SymbolType {
        self.symbol_type
    }

    /// Returns the visibility info of this symbol
    pub fn get_info(&self) -> SymbolInfo {
        self.info
    }

    /// Sets the symbol's name's index into the Symbol String Table
    pub fn set_name_index(&mut self, index: usize) {
        self.name_index = index;
    }

    /// Sets the symbol's value's index into the Symbol Data Table
    pub fn set_value_index(&mut self, index: usize) {
        self.value_index = index;
    }

    /// Returns the symbol's value index
    pub fn get_value_index(&self) -> usize {
        self.value_index
    }

    /// Returns the index of the section that this symbol refers to
    pub fn get_section_index(&self) -> usize {
        self.section_index
    }

    /// Returns a string representation of this symbol that is mostly only useful for hashing
    pub fn to_string(&self) -> String {
        format!("{}:{}:{}:{:?}:{:?}:{}", self.name, self.value_index, self.size, self.info, self.symbol_type, self.section_index)
    }

}

pub struct SymbolDataSection {
    values: Vec<KOSValue>,
    name: String,
    size: u32,
    hash_to_index: HashMap<u64, usize>,
}

impl SymbolDataSection {

    pub fn new(name: &str) -> SymbolDataSection {
        SymbolDataSection {
            values: Vec::new(),
            name: name.to_owned(),
            size: 0,
            hash_to_index: HashMap::new(),
        }
    }

    pub fn read(reader: &mut KOFileReader, header: &SectionHeader) -> Result<SymbolDataSection, Box<dyn Error>> {

        let mut symdata = SymbolDataSection::new(header.name());

        while symdata.size() < header.section_size() {
            let val = KOSValue::read(reader)?;

            symdata.add_no_check(val);
        }

        if symdata.size() > header.section_size() {
            return Err("Error reading symbol data table, size mismatch".into());
        }

        Ok(symdata)
    }

    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        for value in self.values.iter() {
            value.write(writer)?;
        }

        Ok(())
    }

    pub fn size(&self) -> u32 {
        self.size
    }

    pub fn add(&mut self, value: KOSValue) -> usize {
        let mut hasher = DefaultHasher::new();
        value.hash(&mut hasher);
        let val_hash = hasher.finish();

        let val_index = self.values.len();

        // This checks if a KOSValue that is the same has already been added
        match self.hash_to_index.get(&val_hash) {
            // If so, we just return the index of that value
            Some(index) => { return *index; },
            // If not
            None => {
                // Insert the hash and index into our table
                self.hash_to_index.insert(val_hash, val_index);

                // Then add the value
                self.add_no_check(value);
            }
        }

        val_index
    }

    pub fn add_no_check(&mut self, value: KOSValue) -> usize {
        self.values.push(value);

        self.size += self.values.last().unwrap().size() as u32;

        self.values.len() - 1
    }

    pub fn get(&self, index: usize) -> Result<&KOSValue, Box<dyn Error>>{
        match self.values.get(index) {
            Some(s) => Ok(s),
            None => Err(format!("Could not find value at index {} in symbol data section.", index).into()),
        }
    }

    pub fn get_values(&self) -> &Vec<KOSValue> {
        &self.values
    }

    pub fn name(&self) -> &String {
        &self.name
    }

}

pub struct RelSection {
    instructions: Vec<RelInstruction>,
    name: String,
    size: u32,
}

impl RelSection {

    pub fn new(name: &str) -> RelSection {
        RelSection {
            instructions: Vec::new(),
            name: name.to_owned(),
            size: 0
        }
    }

    pub fn read(reader: &mut KOFileReader, header: &SectionHeader) -> Result<RelSection, Box<dyn Error>> {
        let mut rel_section = RelSection::new(header.name());

        while rel_section.size() < header.section_size() {
            rel_section.add(RelInstruction::read(reader)?);
        }

        if rel_section.size() > header.section_size() {
            return Err("Error reading relocatable instruction section, size mismatch".into());
        }

        Ok(rel_section)
    }

    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        for instruction in self.instructions.iter() {
            instruction.write(writer)?;
        }

        Ok(())
    }

    pub fn size(&self) -> u32 {
        self.size
    }

    pub fn name(&self) -> &String {
        &self.name
    }

    pub fn add(&mut self, instr: RelInstruction) -> usize {
        self.instructions.push(instr);

        self.size += self.instructions.last().unwrap().size();

        self.instructions.len() - 1
    }

    pub fn get(&self, index: usize) -> Result<&RelInstruction, Box<dyn Error>> {
        match self.instructions.get(index) {
            Some(i) => Ok(i),
            None => Err(format!("Could not find instruction at index {} in relocatable instruction section.", index).into()),
        }
    }

    pub fn get_instructions(&self) -> &Vec<RelInstruction> {
        &self.instructions
    }

}

pub struct DebugSection {
    entries: Vec<DebugEntry>,
    name: String,
    size: u32,
}

impl DebugSection {
    
    pub fn new(name: &str) -> DebugSection {
        DebugSection {
            entries: Vec::new(),
            name: name.to_owned(),
            size: 0
        }
    }

    pub fn read(reader: &mut KOFileReader, header: &SectionHeader) -> Result<DebugSection, Box<dyn Error>> {
        let mut debug_section = DebugSection::new(header.name());

        while debug_section.size() < header.section_size() {
            debug_section.add(DebugEntry::read(reader)?);
        }

        if debug_section.size() > header.section_size() {
            return Err("Error reading debug information section, size mismatch".into());
        }

        Ok(debug_section)
    }

    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        for entry in self.entries.iter() {
            entry.write(writer)?;
        }

        Ok(())
    }

    pub fn size(&self) -> u32 {
        self.size
    }

    pub fn name(&self) -> &String {
        &self.name
    }

    pub fn add(&mut self, entry: DebugEntry) -> usize {
        self.entries.push(entry);

        self.size += self.entries.last().unwrap().size();

        self.entries.len() - 1
    }

    pub fn get(&self, index: usize) -> Result<&DebugEntry, Box<dyn Error>> {
        match self.entries.get(index) {
            Some(e) => Ok(e),
            None => Err(format!("Could not find debug entry at index {} in debug information section.", index).into()),
        } 
    }

    pub fn get_entries(&self) -> &Vec<DebugEntry> {
        &self.entries
    }

}

pub struct DebugEntry {
    line: u16,
    num_ranges: u8,
    ranges: Vec<(u32, u32)>
}

impl DebugEntry {

    pub fn new(line: u16, ranges: Vec<(u32, u32)>) -> DebugEntry {
        DebugEntry {
            line,
            num_ranges: ranges.len() as u8,
            ranges
        }
    }

    pub fn read(reader: &mut KOFileReader) -> Result<DebugEntry, Box<dyn Error>> {

        let line = reader.read_uint16()?;

        let num_ranges = reader.next()?;

        let mut ranges = Vec::with_capacity(num_ranges as usize);

        for _ in 0..num_ranges {
            let range_start = reader.read_uint32()?;
            let range_stop = reader.read_uint32()?;

            ranges.push( (range_start, range_stop) );
        }

        Ok(DebugEntry::new(line, ranges))

    }

    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        writer.write_uint16(self.line)?;

        writer.write(self.num_ranges)?;

        for (range_start, range_stop) in self.ranges.iter() {
            writer.write_uint32(*range_start)?;
            writer.write_uint32(*range_stop)?;
        }

        Ok(())
    }

    pub fn size(&self) -> u32 {
        3 + self.num_ranges as u32 * 8
    }

}

#[derive(Debug, Clone, PartialEq)]
pub enum KOSValue {
    NULL,
    BOOL(bool),
    BYTE(i8),
    INT16(i16),
    INT32(i32),
    FLOAT(f32),
    DOUBLE(f64),
    STRING(String),
    ARGMARKER,
    SCALARINT(i32),
    SCALARDOUBLE(f64),
    BOOLEANVALUE(bool),
    STRINGVALUE(String),
}

impl Hash for KOSValue {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let s = format!("{:?}", self);
        s.hash(state);
    }
}

impl KOSValue {

    pub fn read(reader: &mut KOFileReader) -> Result<KOSValue, Box<dyn Error>> {
        let value_type: usize = reader.next()? as usize;

        Ok(match value_type {
            0 => KOSValue::NULL,
            1 => KOSValue::BOOL(reader.read_boolean()?),
            2 => KOSValue::BYTE(reader.read_byte()?),
            3 => KOSValue::INT16(reader.read_int16()?),
            4 => KOSValue::INT32(reader.read_int32()?),
            5 => KOSValue::FLOAT(reader.read_float()?),
            6 => KOSValue::DOUBLE(reader.read_double()?),
            7 => KOSValue::STRING(reader.read_kos_string()?),
            8 => KOSValue::ARGMARKER,
            9 => KOSValue::SCALARINT(reader.read_int32()?),
            10 => KOSValue::SCALARDOUBLE(reader.read_double()?),
            11 => KOSValue::BOOLEANVALUE(reader.read_boolean()?),
            12 => KOSValue::STRINGVALUE(reader.read_kos_string()?),
            _ => return Err(format!("Unknown kOS value type encountered: {:x}", value_type).into()),
        })
    }

    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        match self {
            KOSValue::NULL => writer.write(0)?,
            KOSValue::BOOL(b) => {
                writer.write(1)?;
                writer.write_boolean(*b)?;
            },
            KOSValue::BYTE(b) => {
                writer.write(2)?;
                writer.write_byte(*b)?;
            },
            KOSValue::INT16(i) => {
                writer.write(3)?;
                writer.write_int16(*i)?;
            },
            KOSValue::INT32(i) => {
                writer.write(4)?;
                writer.write_int32(*i)?;
            },
            KOSValue::FLOAT(f) => {
                writer.write(5)?;
                writer.write_float(*f)?;
            },
            KOSValue::DOUBLE(d) => {
                writer.write(6)?;
                writer.write_double(*d)?;
            },
            KOSValue::STRING(s) => {
                writer.write(7)?;
                writer.write_kos_string(s)?;
            },
            KOSValue::ARGMARKER => writer.write(8)?,
            KOSValue::SCALARINT(i) => {
                writer.write(9)?;
                writer.write_int32(*i)?;
            },
            KOSValue::SCALARDOUBLE(d) => {
                writer.write(10)?;
                writer.write_double(*d)?;
            },
            KOSValue::BOOLEANVALUE(b) => {
                writer.write(11)?;
                writer.write_boolean(*b)?;
            },
            KOSValue::STRINGVALUE(s) => {
                writer.write(12)?;
                writer.write_kos_string(s)?;
            }
        }

        Ok(())
    }

    pub fn size(&self) -> u16 {
        match self {
            KOSValue::NULL => 1,
            KOSValue::BOOL(_) => 2,
            KOSValue::BYTE(_) => 2,
            KOSValue::INT16(_) => 3,
            KOSValue::INT32(_) => 5,
            KOSValue::FLOAT(_) => 5,
            KOSValue::DOUBLE(_) => 9,
            KOSValue::STRING(s) => s.len() as u16 + 2,
            KOSValue::ARGMARKER => 1,
            KOSValue::SCALARINT(_) => 5,
            KOSValue::SCALARDOUBLE(_) => 9,
            KOSValue::BOOLEANVALUE(_) => 2,
            KOSValue::STRINGVALUE(s) => s.len() as u16 + 2
        }
    }

}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolInfo {
    LOCAL,
    GLOBAL,
    EXTERN
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolType {
    NOTYPE,
    OBJECT,
    FUNC,
    SECTION
}

impl SymbolInfo {

    pub fn to_byte(&self) -> u8 {
        match self {
            SymbolInfo::LOCAL => 0,
            SymbolInfo::GLOBAL => 1,
            SymbolInfo::EXTERN => 2,
        }
    }

    pub fn from(byte: u8) -> Result<SymbolInfo, Box<dyn Error>>{
        match byte {
            0 => Ok(SymbolInfo::LOCAL),
            1 => Ok(SymbolInfo::GLOBAL),
            2 => Ok(SymbolInfo::EXTERN),
            b => Err(format!("Section type of {} is not a valid section type.", b).into())
        }
    }

}

impl SymbolType {

    pub fn to_byte(&self) -> u8 {
        match self {
            SymbolType::NOTYPE => 0,
            SymbolType::OBJECT => 1,
            SymbolType::FUNC => 2,
            SymbolType::SECTION => 3,
        }
    }

    pub fn from(byte: u8) -> Result<SymbolType, Box<dyn Error>>{
        match byte {
            0 => Ok(SymbolType::NOTYPE),
            1 => Ok(SymbolType::OBJECT),
            2 => Ok(SymbolType::FUNC),
            3 => Ok(SymbolType::SECTION),
            b => Err(format!("Section type of {} is not a valid section type.", b).into())
        }
    }

}