/
pass3.c
64 lines (49 loc) · 1.62 KB
/
pass3.c
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
/**
** \file
** \brief Third pass logic of the assembler.
** \author Sandor Zsuga (Jubatian)
** \copyright 2013 - 2014, GNU GPLv3 (version 3 of the GNU General Public
** License) extended as RRPGEvt (temporary version of the RRPGE
** License): see LICENSE.GPLv3 and LICENSE.RRPGEvt in the project
** root.
** \date 2014.10.22
*/
#include "pass3.h"
/* Sections to process into the output file */
static const auint pass3_secout[4] = {SECT_HEAD, SECT_DESC, SECT_CODE, SECT_DATA};
/* Executes the third pass. This combines the application components prepared
** in pass 2 with the FILE section binary data blocks into a new application
** binary file. Returns nonzero on failure. */
auint pass3_run(FILE* obi, symtab_t* stb, bindata_t* bdt)
{
uint8 s[80];
uint8 e[80];
section_t* sec = symtab_getsectob(stb);
auint i;
auint j;
auint ssi;
uint16 const* d;
uint8 c[2];
/* Write out sections in order. The FILE section will have zero size here, so
** no problem including it. */
for (i = 0U; i < (sizeof(pass3_secout) / sizeof(pass3_secout[0])); i++){
section_setsect(sec, pass3_secout[i]);
d = section_getdata(sec);
ssi = section_getsize(sec);
for (j = 0U; j < ssi; j++){
c[0] = d[j] >> 8;
c[1] = d[j] & 0xFFU;
if (fwrite(&c[0], 1U, 2U, obi) != 2U){ goto fault_wrt; }
}
}
/* Write out binary data */
bindata_out(bdt, obi);
/* Done */
return 0U;
fault_wrt:
strerror_r(errno, (char*)(&e[0]), 80U);
e[79] = 0U;
snprintf((char*)(&s[0]), 80U, "Failed to write target binary: %s", (char const*)(&e[0]));
fault_printgen(FAULT_FAIL, &s[0]);
return 1U;
}