|
| 1 | +/*************************************************************** |
| 2 | + * PRXTool : Utility for PSP executables. |
| 3 | + * (c) TyRaNiD 2k5 |
| 4 | + * |
| 5 | + * SerializePrxToMap.C - Implementation of a class to serialize |
| 6 | + * a loaded PRX file to a PS2DIS Map file. |
| 7 | + ***************************************************************/ |
| 8 | + |
| 9 | +#include <stdio.h> |
| 10 | +#include "SerializePrxToMap.h" |
| 11 | + |
| 12 | +/* Build a name from a base and extention */ |
| 13 | +static const char *BuildName(const char* base, const char *ext) |
| 14 | +{ |
| 15 | + static char str_export[512]; |
| 16 | + |
| 17 | + snprintf(str_export, sizeof(str_export), "%s_%s", base, ext); |
| 18 | + |
| 19 | + return str_export; |
| 20 | +} |
| 21 | + |
| 22 | +static void PrintOffset(FILE *fp, unsigned int addr) |
| 23 | +{ |
| 24 | + fprintf(fp, "%08x:\n", addr); |
| 25 | +} |
| 26 | + |
| 27 | +static void PrintComment(FILE *fp, const char *text) |
| 28 | +{ |
| 29 | + fprintf(fp, "# %s\n", text); |
| 30 | +} |
| 31 | + |
| 32 | +CSerializePrxToMap::CSerializePrxToMap(FILE *fpOut) |
| 33 | +{ |
| 34 | + m_fpOut = fpOut; |
| 35 | +} |
| 36 | + |
| 37 | +CSerializePrxToMap::~CSerializePrxToMap() |
| 38 | +{ |
| 39 | + fflush(m_fpOut); |
| 40 | +} |
| 41 | + |
| 42 | +bool CSerializePrxToMap::StartFile() |
| 43 | +{ |
| 44 | + return true; |
| 45 | +} |
| 46 | + |
| 47 | +bool CSerializePrxToMap::EndFile() |
| 48 | +{ |
| 49 | + /* Do nothing */ |
| 50 | + return true; |
| 51 | +} |
| 52 | + |
| 53 | +bool CSerializePrxToMap::StartPrx(const char* szFilename, const PspModule *mod, u32 iSMask) |
| 54 | +{ |
| 55 | + int i; |
| 56 | + u32 addr; |
| 57 | + |
| 58 | + PrintComment(m_fpOut, "Generated by prxtool"); |
| 59 | + PrintComment(m_fpOut, "Make sure to \"Load From Address 0xA0\" to skip the ELF header"); |
| 60 | + PrintComment(m_fpOut, "Make sure to load the module as plain binary, not as ELF"); |
| 61 | + fprintf(m_fpOut, "# File: %s\n", szFilename); |
| 62 | + |
| 63 | + addr = mod->addr; |
| 64 | + |
| 65 | + PrintOffset(m_fpOut, addr); |
| 66 | + fprintf(m_fpOut, ".word\t_module_flags\n"); |
| 67 | + fprintf(m_fpOut, ".byte\t_module_name\n"); |
| 68 | + for(i=0; i < (sizeof(mod->name)-2); i++) |
| 69 | + fprintf(m_fpOut, ".byte\n"); |
| 70 | + fprintf(m_fpOut, ".word\t_module_gp\n"); |
| 71 | + fprintf(m_fpOut, ".word\t_module_exports\n"); |
| 72 | + fprintf(m_fpOut, ".word\t_module_exp_end\n"); |
| 73 | + fprintf(m_fpOut, ".word\t_module_imports\n"); |
| 74 | + fprintf(m_fpOut, ".word\t_module_imp_end\n"); |
| 75 | + |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +bool CSerializePrxToMap::EndPrx() |
| 80 | +{ |
| 81 | + /* Do nothing */ |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +bool CSerializePrxToMap::StartSects() |
| 86 | +{ |
| 87 | + /* Do nothing */ |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +bool CSerializePrxToMap::SerializeSect(int num, ElfSection §) |
| 92 | +{ |
| 93 | + u32 shFlags; |
| 94 | + u32 shType; |
| 95 | + u32 shAddr; |
| 96 | + u32 shSize; |
| 97 | + const char *pName; |
| 98 | + |
| 99 | + shFlags = sect.iFlags; |
| 100 | + shType = sect.iType; |
| 101 | + shAddr = sect.iAddr; |
| 102 | + shSize = sect.iSize; |
| 103 | + pName = sect.szName; |
| 104 | + |
| 105 | + /* Check if the section is loadable */ |
| 106 | + if((shFlags & SHF_ALLOC) && ((shType == SHT_PROGBITS) || (shType == SHT_NOBITS))) |
| 107 | + { |
| 108 | + PrintOffset(m_fpOut, shAddr); |
| 109 | + fprintf(m_fpOut, ".word\t%s\t;", pName); |
| 110 | + |
| 111 | + if(shFlags & SHF_EXECINSTR) |
| 112 | + { |
| 113 | + fprintf(m_fpOut, " SEG_CODE"); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + if(shType == SHT_NOBITS) |
| 118 | + { |
| 119 | + fprintf(m_fpOut, " SEG_BSS"); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + fprintf(m_fpOut, " SEG_DATA"); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + fprintf(m_fpOut, " 0x%08x - 0x%08x\n", shAddr, shAddr + shSize); |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | +bool CSerializePrxToMap::EndSects() |
| 134 | +{ |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | +bool CSerializePrxToMap::StartImports() |
| 139 | +{ |
| 140 | + return true; |
| 141 | +} |
| 142 | + |
| 143 | +bool CSerializePrxToMap::SerializeImport(int num, const PspLibImport *imp) |
| 144 | +{ |
| 145 | + char str_import[128]; |
| 146 | + int iLoop; |
| 147 | + u32 addr; |
| 148 | + snprintf(str_import, sizeof(str_import), "import%d", num); |
| 149 | + |
| 150 | + addr = imp->addr; |
| 151 | + |
| 152 | + if(imp->stub.name != 0) |
| 153 | + { |
| 154 | + PrintOffset(m_fpOut, addr); |
| 155 | + fprintf(m_fpOut, ".word\t%s\t; %s\n", imp->name, BuildName(str_import, "name")); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + PrintOffset(m_fpOut, addr); |
| 160 | + fprintf(m_fpOut, ".word\t%s\t; %s\n", str_import, BuildName(str_import, "name")); |
| 161 | + } |
| 162 | + |
| 163 | + fprintf(m_fpOut, ".word\t%s\n", BuildName(str_import, "flags")); |
| 164 | + fprintf(m_fpOut, ".word\t%s\n", BuildName(str_import, "counts")); |
| 165 | + fprintf(m_fpOut, ".word\t%s\n", BuildName(str_import, "nids")); |
| 166 | + fprintf(m_fpOut, ".word\t%s\n", BuildName(str_import, "funcs")); |
| 167 | + |
| 168 | + for(iLoop = 0; iLoop < imp->f_count; iLoop++) |
| 169 | + { |
| 170 | + PrintOffset(m_fpOut, imp->funcs[iLoop].nid_addr); |
| 171 | + fprintf(m_fpOut, ".word\t%s\t; NID %08x\n", BuildName(str_import, imp->funcs[iLoop].name), imp->funcs[iLoop].nid); |
| 172 | + |
| 173 | + PrintOffset(m_fpOut, imp->funcs[iLoop].addr); |
| 174 | + fprintf(m_fpOut, ".code\t%s\n", imp->funcs[iLoop].name); |
| 175 | + } |
| 176 | + |
| 177 | + for(iLoop = 0; iLoop < imp->v_count; iLoop++) |
| 178 | + { |
| 179 | + |
| 180 | + PrintOffset(m_fpOut, imp->funcs[iLoop].nid_addr); |
| 181 | + fprintf(m_fpOut, ".word\t%s\t; NID %08x\n", BuildName(str_import, imp->vars[iLoop].name), imp->vars[iLoop].nid); |
| 182 | + |
| 183 | + PrintOffset(m_fpOut, imp->vars[iLoop].nid_addr + ((imp->v_count + imp->f_count) * 4)); |
| 184 | + fprintf(m_fpOut, ".word\t%s\n", imp->vars[iLoop].name); |
| 185 | + } |
| 186 | + |
| 187 | + return true; |
| 188 | +} |
| 189 | + |
| 190 | +bool CSerializePrxToMap::EndImports() |
| 191 | +{ |
| 192 | + return true; |
| 193 | +} |
| 194 | + |
| 195 | +bool CSerializePrxToMap::StartExports() |
| 196 | +{ |
| 197 | + return true; |
| 198 | +} |
| 199 | + |
| 200 | +bool CSerializePrxToMap::SerializeExport(int num, const PspLibExport *exp) |
| 201 | +{ |
| 202 | + char str_export[128]; |
| 203 | + int iLoop; |
| 204 | + u32 addr; |
| 205 | + snprintf(str_export, sizeof(str_export), "export_%d", num); |
| 206 | + |
| 207 | + addr = exp->addr; |
| 208 | + |
| 209 | + if(exp->stub.name != 0) |
| 210 | + { |
| 211 | + PrintOffset(m_fpOut, addr); |
| 212 | + fprintf(m_fpOut, ".word\t%s\t; %s\n", exp->name, BuildName(str_export, "name")); |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + PrintOffset(m_fpOut, addr); |
| 217 | + fprintf(m_fpOut, ".word\t%s\t; %s\n", str_export, BuildName(str_export, "name")); |
| 218 | + } |
| 219 | + |
| 220 | + fprintf(m_fpOut, ".word\t%s\n", BuildName(str_export, "flags")); |
| 221 | + fprintf(m_fpOut, ".word\t%s\n", BuildName(str_export, "counts")); |
| 222 | + fprintf(m_fpOut, ".word\t%s\n", BuildName(str_export, "exports")); |
| 223 | + |
| 224 | + for(iLoop = 0; iLoop < exp->f_count; iLoop++) |
| 225 | + { |
| 226 | + PrintOffset(m_fpOut, exp->funcs[iLoop].nid_addr); |
| 227 | + fprintf(m_fpOut, ".word\t%s\t; NID %08x\n", BuildName(str_export, exp->funcs[iLoop].name), exp->funcs[iLoop].nid); |
| 228 | + |
| 229 | + PrintOffset(m_fpOut, exp->funcs[iLoop].nid_addr + ((exp->v_count + exp->f_count) * 4)); |
| 230 | + fprintf(m_fpOut, ".word\n"); |
| 231 | + |
| 232 | + PrintOffset(m_fpOut, exp->funcs[iLoop].addr); |
| 233 | + fprintf(m_fpOut, ".code\t%s\n", exp->funcs[iLoop].name); |
| 234 | + } |
| 235 | + |
| 236 | + for(iLoop = 0; iLoop < exp->v_count; iLoop++) |
| 237 | + { |
| 238 | + PrintOffset(m_fpOut, exp->funcs[iLoop].nid_addr); |
| 239 | + fprintf(m_fpOut, ".word\t%s\t; NID %08x\n", BuildName(str_export, exp->vars[iLoop].name), exp->vars[iLoop].nid); |
| 240 | + |
| 241 | + PrintOffset(m_fpOut, exp->vars[iLoop].nid_addr + ((exp->v_count + exp->f_count) * 4)); |
| 242 | + fprintf(m_fpOut, ".word\t%s\n", exp->vars[iLoop].name); |
| 243 | + } |
| 244 | + |
| 245 | + return true; |
| 246 | +} |
| 247 | + |
| 248 | +bool CSerializePrxToMap::EndExports() |
| 249 | +{ |
| 250 | + return true; |
| 251 | +} |
| 252 | + |
| 253 | +bool CSerializePrxToMap::StartRelocs() |
| 254 | +{ |
| 255 | + return true; |
| 256 | +} |
| 257 | + |
| 258 | +bool CSerializePrxToMap::SerializeReloc(int count, const ElfReloc *rel) |
| 259 | +{ |
| 260 | + ElfSection *pDataSect, *pTextSect; |
| 261 | + |
| 262 | + pDataSect = m_currPrx->ElfFindSection(".data"); |
| 263 | + pTextSect = m_currPrx->ElfFindSection(".text"); |
| 264 | + |
| 265 | + fprintf(stderr, "Reloc count %d, %s, data %p, text %p\n", count, rel->secname, pDataSect, pTextSect); |
| 266 | + return true; |
| 267 | +} |
| 268 | + |
| 269 | +bool CSerializePrxToMap::EndRelocs() |
| 270 | +{ |
| 271 | + return true; |
| 272 | +} |
| 273 | + |
0 commit comments