Beispiel #1
0
bool CCBuffer::isReadable(unsigned int u_len) const
{
	BEGIN_IF(LH(LE(_u_content_size, 0), LE(u_len, 0)))
	DO_RETURN_FLASE;
	END_IF
	DO_RETURN_R(LXE(CA(_u_read_pos, u_len), _u_content_size));
}
Beispiel #2
0
int main()
{
	// just call everyone's function
	ZGN();
	QRJ();
	LH();
	JDQ();
	return 0;
}
Beispiel #3
0
UDCell ummul (UCell a, UCell b)	/* unsigned multiply, mixed precision */
{
  UDCell res;
  UCell m,ul,lu,uu;

  res.lo = a*b;
/*ll = LH(a)*LH(b); dead code */
  ul = UH(a)*LH(b);
  lu = LH(a)*UH(b);
  uu = UH(a)*UH(b);
  m = ul+lu;
  res.hi = (uu
	    + L2U(m<ul) /* the carry of ul+lu */
	    + UH(m)
	    + (res.lo<L2U(m)) /* the carry of ll+L2U(m) */
	    );
  return res;
}
Beispiel #4
0
// displayLoadInfo - Displays low level loader type info.
static void displayLoadInfo(MZ_Header *hdr)
{
//    printf("File type is %s\n", (prog.fCOM) ? "COM" : "EXE");
//    if (!prog.fCOM) {
    printf("Signature            = %04X\n",       hdr->signature);
    printf("File size %% 512     = %04X\n",       hdr->lastPageSize);
    printf("File size / 512      = %04X pages\n", hdr->numPages);
    printf("# relocation items   = %04X\n",       hdr->numReloc);
    printf("Offset to load image = %04X paras\n", hdr->numParaHeader);
    printf("Minimum allocation   = %04X paras\n", hdr->minAlloc);
    printf("Maximum allocation   = %04X paras\n", hdr->maxAlloc);
//    }
    size_t size = prog.cbImage - sizeof(PSP);
    printf("Load image size      = %04lX (%lu bytes)\n", size, size);
    printf("Initial SS:SP        = %04X:%04X\n",  prog.initSS, prog.initSP);
    printf("Initial CS:IP        = %04X:%04X\n",  prog.initCS, prog.initIP);

    if (option.VeryVerbose && prog.cReloc) {
        printf("\nRelocation Table\n");
        for (int i = 0; i < prog.cReloc; i++)
            printf("%06X -> [%04X]\n", prog.relocTable[i], LH(prog.Image + prog.relocTable[i]));
    }
    printf("\n");
}
Beispiel #5
0
static uint32_t sub_pixel_sse_diff_4width_h_msa(const uint8_t *src,
                                                int32_t src_stride,
                                                const uint8_t *dst,
                                                int32_t dst_stride,
                                                const uint8_t *filter,
                                                int32_t height,
                                                int32_t *diff) {
  int16_t filtval;
  uint32_t loop_cnt;
  uint32_t ref0, ref1, ref2, ref3;
  v16u8 filt0, ref = { 0 };
  v16i8 src0, src1, src2, src3;
  v16i8 mask = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8 };
  v8u16 vec0, vec1, vec2, vec3;
  v8u16 const255;
  v8i16 avg = { 0 };
  v4i32 vec, var = { 0 };

  filtval = LH(filter);
  filt0 = (v16u8)__msa_fill_h(filtval);

  const255 = (v8u16)__msa_ldi_h(255);

  for (loop_cnt = (height >> 2); loop_cnt--;) {
Beispiel #6
0
static void
usage(FILE *str, const char *a0, int ec)
{
	#define XSTR(s) STR(s)
	#define STR(s) #s
	#define LH(STR) fputs(STR "\n", str)
	LH("==========================");
	LH("== iwat "PACKAGE_VERSION" - IRC wat ==");
	LH("==========================");
	fprintf(str, "usage: %s [-vchtkSnufFspPTCwlLb] <hostspec> "
	    "[<hostspec> ...]\n", a0);
	LH("");
	LH("\t<hostspec> specifies an IRC server to connect against");
	LH("\t           multiple hostspecs may be given.");
	LH("\t\thostspec := srvaddr[:port]['/ssl']");
	LH("\t\tsrvaddr  := ip4addr|ip6addr|dnsname");
	LH("\t\tport     := int(0..65535)");
	LH("\t\tip4addr  := 'aaa.bbb.ccc.ddd'");
	LH("\t\tip6addr  :=  '[aa:bb:cc::dd:ee]'");
	LH("\t\tdnsname  := 'irc.example.org'");
	LH("");
	LH("\t-v: Increase verbosity on stderr (use -vv or -vvv for more)");
	LH("\t-q: Decrease verbosity on stderr (use -qq or -qqq for less)");
	LH("\t-h: Display usage statement and terminate");
	LH("\t-k: Keep trying to connect, if first connect/logon fails");
	LH("\t-r: Reconnect on disconnect, rather than terminating");
	LH("");
	LH("\t-n <str>: Use <str> as nick. Subject to mutilation if N/A");
	LH("\t-u <str>: Use <str> as (IRC) username/ident");
	LH("\t-f <str>: Use <str> as (IRC) fullname");
	LH("\t-b <int>: Send heart beat PING every <int> ms."
	    "[def: "XSTR(DEF_HEARTBEAT_MS)"]");
	LH("\t-W <int>: Wait <int> seconds between attempts to connect."
	    "[def: "XSTR(DEF_CONFAILWAIT_S)"]");
	LH("\t-p <str>: Use <str> as server password");
	LH("\t-P <pxspec>: Use <pxspec> as proxy. See below for format.");
	LH("\t-T <int>[:<int>]: Connect/Logon hard[:soft]-timeout in ms."
	    "[def: "XSTR(DEF_CONTO_HARD_MS)":"XSTR(DEF_CONTO_SOFT_MS)"]");
	LH("");
	LH("\t<pxspec> specifies a proxy and mostly works like hostspec:");
	LH("\t\tpxspec   := pxtype:hostspec");
	LH("\t\tpxtype   := 'HTTP' | 'SOCKS4' | 'SOCKS5'");
	LH("");
	LH("(C) 2012-15, Timo Buhrmester (contact: #fstd @ irc.freenode.org)");
	#undef LH
	#undef STR
	#undef XSTR
	exit(ec);
}
Beispiel #7
0
/*===========================================================================*
 *				void *slaballoc				     *
 *===========================================================================*/
void *slaballoc(int bytes)
{
	int i;
	int count = 0;
	struct slabheader *s;
	struct slabdata *firstused;

	SLABSANITYCHECK(SCL_FUNCTIONS);

	/* Retrieve entry in slabs[]. */
	GETSLAB(bytes, s);
	vm_assert(s);

	/* To make the common case more common, make space in the 'used'
	 * queue first.
	 */
	if(!LH(s, LIST_USED)) {
		/* Make sure there is something on the freelist. */
	SLABSANITYCHECK(SCL_DETAIL);
		if(!LH(s, LIST_FREE)) {
			struct slabdata *nd = newslabdata(LIST_FREE);
	SLABSANITYCHECK(SCL_DETAIL);
			if(!nd) return NULL;
			ADDHEAD(nd, s, LIST_FREE);
	SLABSANITYCHECK(SCL_DETAIL);
		}


	SLABSANITYCHECK(SCL_DETAIL);
		MOVEHEAD(s, LIST_FREE, LIST_USED);
	SLABSANITYCHECK(SCL_DETAIL);

	}
	SLABSANITYCHECK(SCL_DETAIL);

	vm_assert(s);
	firstused = LH(s, LIST_USED);
	vm_assert(firstused);
	vm_assert(firstused->sdh.magic1 == MAGIC1);
	vm_assert(firstused->sdh.magic2 == MAGIC2);
	vm_assert(firstused->sdh.nused < ITEMSPERPAGE(bytes));

	for(i = firstused->sdh.freeguess;
		count < ITEMSPERPAGE(bytes); count++, i++) {
	SLABSANITYCHECK(SCL_DETAIL);
		i = i % ITEMSPERPAGE(bytes);

		if(!GETBIT(firstused, i)) {
			struct slabdata *f;
			char *ret;
			SETBIT(firstused, i);
	SLABSANITYCHECK(SCL_DETAIL);
			if(firstused->sdh.nused == ITEMSPERPAGE(bytes)) {
	SLABSANITYCHECK(SCL_DETAIL);
				MOVEHEAD(s, LIST_USED, LIST_FULL);
	SLABSANITYCHECK(SCL_DETAIL);
			}
	SLABSANITYCHECK(SCL_DETAIL);
			ret = ((char *) firstused->data) + i*bytes;

#if SANITYCHECKS
			nojunkwarning++;
			slabunlock(ret, bytes);
			nojunkwarning--;
			vm_assert(!nojunkwarning);
			*(u32_t *) ret = NOJUNK;
			slablock(ret, bytes);
#endif
			SLABSANITYCHECK(SCL_FUNCTIONS);
			SLABDATAUSE(firstused, firstused->sdh.freeguess = i+1;);

#if SANITYCHECKS
	if(bytes >= SLABSIZES+MINSIZE) {
		printk("slaballoc: odd, bytes %d?\n", bytes);
	}
			if(!slabsane_f(__FILE__, __LINE__, ret, bytes))
				vm_panic("slaballoc: slabsane failed", NO_NUM);
#endif

			return ret;
		}

	SLABSANITYCHECK(SCL_DETAIL);

	}
bool ExeBinaryFile::RealLoad(const char* sName)
{
	FILE    *fp;
	int     i, cb;
	uint8_t buf[4];
	int     fCOM;

        m_pFileName = sName;

	// Always just 3 sections
	m_pSections = new SectionInfo[3];
	if (m_pSections == 0)
	{
		fprintf(stderr, "Could not allocate section information\n");
		return 0;
	}
	m_iNumSections = 3;
	m_pHeader = new exeHeader;
	if (m_pHeader == 0)
	{
		fprintf(stderr, "Could not allocate header memory\n");
		return 0;
	}

	/* Open the input file */
	if ((fp = fopen(sName, "rb")) == NULL)
	{
		fprintf(stderr, "Could not open file %s\n", sName);
		return 0;
	}

	/* Read in first 2 bytes to check EXE signature */
	if (fread(m_pHeader, 1, 2, fp) != 2)
	{
		fprintf(stderr, "Cannot read file %s\n", sName);
		return 0;
	}

	// Check for the "MZ" exe header
	if (! (fCOM = (m_pHeader->sigLo != 0x4D || m_pHeader->sigHi != 0x5A)))
	{
		/* Read rest of m_pHeader */
		fseek(fp, 0, SEEK_SET);
		if (fread(m_pHeader, sizeof(exeHeader), 1, fp) != 1)
		{
			fprintf(stderr, "Cannot read file %s\n", sName);
			return 0;
		}

		/* This is a typical DOS kludge! */
		if (LH(&m_pHeader->relocTabOffset) == 0x40)
		{
			fprintf(stderr, "Error - NE format executable\n");
			return 0;
		}

		/* Calculate the load module size.
		 * This is the number of pages in the file
		 * less the length of the m_pHeader and reloc table
		 * less the number of bytes unused on last page
		*/
		cb = (uint32_t)LH(&m_pHeader->numPages) * 512 -
			(uint32_t)LH(&m_pHeader->numParaHeader) * 16;
		if (m_pHeader->lastPageSize)
		{
			cb -= 512 - LH(&m_pHeader->lastPageSize);
		}
		
		/* We quietly ignore minAlloc and maxAlloc since for our
		 * purposes it doesn't really matter where in real memory
		 * the m_am would end up.  EXE m_ams can't really rely on
		 * their load location so setting the PSP segment to 0 is fine.
		 * Certainly m_ams that prod around in DOS or BIOS are going
		 * to have to load DS from a constant so it'll be pretty 
		 * obvious.
		*/
		m_cReloc = (uint16_t)LH(&m_pHeader->numReloc);

		/* Allocate the relocation table */
		if (m_cReloc)
		{
			m_pRelocTable = new uint32_t[m_cReloc];
			if (m_pRelocTable == 0)
			{
				fprintf(stderr, "Could not allocate relocation table "
					"(%d entries)\n", m_cReloc);
				return 0;
			}
			fseek(fp, LH(&m_pHeader->relocTabOffset), SEEK_SET);

			/* Read in seg:offset pairs and convert to Image ptrs */
			for (i = 0; i < m_cReloc; i++)
			{
				fread(buf, 1, 4, fp);
				m_pRelocTable[i] = LH(buf) + 
					(((int)LH(buf+2))<<4);
			}
		}

		/* Seek to start of image */
		fseek(fp, (int)LH(&m_pHeader->numParaHeader) * 16, SEEK_SET);

		// Initial PC and SP. Note that we fake the seg:offset by putting
		// the segment in the top half, and offset int he bottom
		m_uInitPC = ((LH(&m_pHeader->initCS)) << 16) + LH(&m_pHeader->initIP);
		m_uInitSP = ((LH(&m_pHeader->initSS)) << 16) + LH(&m_pHeader->initSP);
	}
	else
	{	/* COM file
		 * In this case the load module size is just the file length
		*/
		fseek(fp, 0, SEEK_END);
		cb = ftell(fp);

		/* COM programs start off with an ORG 100H (to leave room for a PSP)
		 * This is also the implied start address so if we load the image
		 * at offset 100H addresses should all line up properly again.
		*/
		m_uInitPC = 0x100;
		m_uInitSP = 0xFFFE;
		m_cReloc = 0;

		fseek(fp, 0, SEEK_SET);
	}

	/* Allocate a block of memory for the image. */
	m_cbImage  = cb;
	m_pImage    = new uint8_t[m_cbImage];

	if (cb != (int)fread(m_pImage, 1, (size_t)cb, fp))
	{
		fprintf(stderr, "Cannot read file %s\n", sName);
		return 0;
	}

	/* Relocate segment constants */
	if (m_cReloc)
	{
		for (i = 0; i < m_cReloc; i++)
		{
			uint8_t *p = &m_pImage[m_pRelocTable[i]];
			uint16_t  w = (uint16_t)LH(p);
			*p++    = (uint8_t)(w & 0x00FF);
			*p      = (uint8_t)((w & 0xFF00) >> 8);
		}
	}

	fclose(fp);

	m_pSections[0].pSectionName = const_cast<char *>("$HEADER");	// Special header section
//	m_pSections[0].fSectionFlags = ST_HEADER;
	m_pSections[0].uNativeAddr = 0;				// Not applicable
	m_pSections[0].uHostAddr = (uint32_t)m_pHeader;
	m_pSections[0].uSectionSize = sizeof(exeHeader);
	m_pSections[0].uSectionEntrySize = 1;		// Not applicable

	m_pSections[1].pSectionName = const_cast<char *>(".text");		// The text and data section
	m_pSections[1].bCode = true;
	m_pSections[1].bData = true;
	m_pSections[1].uNativeAddr = 0;
	m_pSections[1].uHostAddr = (uint32_t)m_pImage;
	m_pSections[1].uSectionSize = m_cbImage;
	m_pSections[1].uSectionEntrySize = 1;		// Not applicable

	m_pSections[2].pSectionName = const_cast<char *>("$RELOC");		// Special relocation section
//	m_pSections[2].fSectionFlags = ST_RELOC;	// Give it a special flag
	m_pSections[2].uNativeAddr = 0;				// Not applicable
	m_pSections[2].uHostAddr = (uint32_t)m_pRelocTable;
	m_pSections[2].uSectionSize = sizeof(uint32_t) * m_cReloc;
	m_pSections[2].uSectionEntrySize = sizeof(uint32_t);

	return 1;

}
ADDRESS ExeBinaryFile::GetEntryPoint()
{
    // Check this...
    return (ADDRESS)((LH(&m_pHeader->initCS) << 4) + LH(&m_pHeader->initIP));
}
Beispiel #10
0
int remove_weak_relocs(struct ElfSection *pReloc, struct ElfSection *pSymbol, struct ElfSection *pString)
{
	int iCount;
	int iMaxSymbol;
	void *pNewRel = NULL;
	Elf32_Rel *pInRel;
	Elf32_Rel *pOutRel;
	Elf32_Sym *pSymData = (Elf32_Sym *) pSymbol->pData;
	char *pStrData = NULL;
	int iOutput;
	int i;

	if(pString != NULL)
	{
		pStrData = (char *) pString->pData;
	}

	iMaxSymbol = pSymbol->iSize / sizeof(Elf32_Sym);
	iCount = pReloc->iSize / sizeof(Elf32_Rel);

	pNewRel = malloc(pReloc->iSize);
	if(pNewRel == NULL)
	{
		return 0;
	}
	pOutRel = (Elf32_Rel *) pNewRel;
	pInRel = (Elf32_Rel *) pReloc->pData;
	iOutput = 0;

	if(g_verbose)
	{
		fprintf(stderr, "[%s] Processing %d relocations, %d symbols\n", pReloc->szName, iCount, iMaxSymbol);
	}

	for(i = 0; i < iCount; i++)
	{
		int iSymbol;

		iSymbol = ELF32_R_SYM(LW(pInRel->r_info));
		if(g_verbose)
		{
			fprintf(stderr, "Relocation %d - Symbol %x\n", iOutput, iSymbol);
		}

		if(iSymbol >= iMaxSymbol)
		{
			fprintf(stderr, "Warning: Ignoring relocation as cannot find matching symbol\n");
		}
		else
		{
			if(g_verbose)
			{
				if(pStrData != NULL)
				{
					fprintf(stderr, "Symbol %d - Name %s info %x ndx %x\n", iSymbol, &pStrData[pSymData[iSymbol].st_name], 
							pSymData[iSymbol].st_info, pSymData[iSymbol].st_shndx);
				}
				else
				{
					fprintf(stderr, "Symbol %d - Name %d info %x ndx %x\n", iSymbol, pSymData[iSymbol].st_name, 
							pSymData[iSymbol].st_info, pSymData[iSymbol].st_shndx);
				}
			}

			/* Remove PC16 relocations (unsupported by PSP, and useless) */
			if(LH(pSymData[iSymbol].st_shndx) == 0 || ELF32_R_TYPE(LW(pInRel->r_info)) == R_MIPS_PC16)
			{
				if(g_verbose)
				{
					fprintf(stderr, "Deleting relocation\n");
				}
			}
			else
			{
				/* We are keeping this relocation, copy it across */
				*pOutRel = *pInRel;
				pOutRel++;
				iOutput++;
			}
		}

		pInRel++;
	}

	/* If we deleted some relocations */
	if(iOutput < iCount)
	{
		int iSize;

		iSize = iOutput * sizeof(Elf32_Rel);
		if(g_verbose)
		{
			fprintf(stderr, "Old relocation size %d, new %d\n", pReloc->iSize, iSize);
		}
		pReloc->iSize = iSize;
		/* If size is zero then delete this section */
		if(iSize == 0)
		{
			pReloc->blOutput = 0;
		}
		else
		{
			/* Copy across the new relocation data */
			memcpy(pReloc->pData, pNewRel, pReloc->iSize);
		}
	}

	free(pNewRel);

	return 1;
}
Beispiel #11
0
/* Validate the ELF header */
int validate_header(unsigned char *data)
{
	Elf32_Ehdr *head;
	int ret = 0;

	head = (Elf32_Ehdr*) data;

	do
	{
		/* Read in the header structure */
		g_elfhead.iMagic = LW(head->e_magic);
		g_elfhead.iClass = head->e_class;
		g_elfhead.iData = head->e_data;
		g_elfhead.iIdver = head->e_idver;
		g_elfhead.iType = LH(head->e_type);
		g_elfhead.iMachine = LH(head->e_machine);
		g_elfhead.iVersion = LW(head->e_version);
		g_elfhead.iEntry = LW(head->e_entry);
		g_elfhead.iPhoff = LW(head->e_phoff);
		g_elfhead.iShoff = LW(head->e_shoff);
		g_elfhead.iFlags = LW(head->e_flags);
		g_elfhead.iEhsize = LH(head->e_ehsize);
		g_elfhead.iPhentsize = LH(head->e_phentsize);
		g_elfhead.iPhnum = LH(head->e_phnum);
		g_elfhead.iShentsize = LH(head->e_shentsize);
		g_elfhead.iShnum = LH(head->e_shnum);
		g_elfhead.iShstrndx = LH(head->e_shstrndx);

		if(g_verbose)
		{
			fprintf(stderr, "Magic %08X, Class %02X, Data %02X, Idver %02X\n", g_elfhead.iMagic,
					g_elfhead.iClass, g_elfhead.iData, g_elfhead.iIdver);
			fprintf(stderr, "Type %04X, Machine %04X, Version %08X, Entry %08X\n", g_elfhead.iType,
					g_elfhead.iMachine, g_elfhead.iVersion, g_elfhead.iEntry);
			fprintf(stderr, "Phoff %08X, Shoff %08X, Flags %08X, Ehsize %08X\n", g_elfhead.iPhoff,
					g_elfhead.iShoff, g_elfhead.iFlags, g_elfhead.iEhsize);
			fprintf(stderr, "Phentsize %04X, Phnum %04X\n", g_elfhead.iPhentsize, g_elfhead.iPhnum);
			fprintf(stderr, "Shentsize %04X, Shnum %08X, Shstrndx %04X\n", g_elfhead.iShentsize,
					g_elfhead.iShnum, g_elfhead.iShstrndx);
		}

		if(g_elfhead.iMagic != ELF_MAGIC)
		{
			fprintf(stderr, "Error, invalid magic in the header\n");
			break;
		}

		if((g_elfhead.iType != ELF_EXEC_TYPE) && (g_elfhead.iType != ELF_PRX_TYPE))
		{
			fprintf(stderr, "Error, not EXEC type elf\n");
			break;
		}

		if(g_elfhead.iMachine != ELF_MACHINE_MIPS)
		{
			fprintf(stderr, "Error, not MIPS type ELF\n");
			break;
		}

		if(g_elfhead.iShnum < g_elfhead.iShstrndx)
		{
			fprintf(stderr, "Error, number of headers is less than section string index\n");
			break;
		}

		ret = 1;
	}
	while(0);

	return ret;
}
Beispiel #12
0
// LoadImage
static void LoadImage(FILE *fp, MZ_Header *hdr)
{
    /* Calculate the load module size. This is the number of pages in the file less the length
       of the header and reloc table less the number of bytes unused on last page */        
    size_t cb = hdr->numPages * 512 - (hdr->numParaHeader * 16);

    if (hdr->lastPageSize)
        cb -= (512 - hdr->lastPageSize);

    /* We quietly ignore minAlloc and maxAlloc since for our purposes it doesn't really matter
       where in real memory the program would end up. EXE programs can't really rely on their
       load location so setting the PSP segment to 0 is fine.
       Certainly programs that prod around in DOS or BIOS are going to have to load DS from
       a constant so it'll be pretty obvious. */
    prog.initCS = hdr->initCS + EXE_RELOCATION;
    prog.initIP = hdr->initIP;
    prog.initSS = hdr->initSS + EXE_RELOCATION;
    prog.initSP = hdr->initSP;
    prog.cReloc = hdr->numReloc;

    // Allocate the relocation table
    if (prog.cReloc) {
        MZ_Reloc *reloc_table = malloc(prog.cReloc * sizeof(MZ_Reloc *)); 
        prog.relocTable = allocMem(prog.cReloc * sizeof(uint32_t));
        fseek(fp, hdr->relocTabOffset, SEEK_SET);

        // Read in seg:offset pairs and convert to Image ptrs
        for (int i = 0; i < prog.cReloc; i++) {
            fread(&reloc_table[i], sizeof(MZ_Reloc), 1, fp);
            prog.relocTable[i] = reloc_table[i].off + ((reloc_table[i].seg + EXE_RELOCATION) << 4);
        }

        free(reloc_table);
    }

    // Seek to start of image
    fseek(fp, hdr->numParaHeader * 16, SEEK_SET);

    // Allocate a block of memory for the program.
    prog.cbImage = cb + sizeof(PSP);
    prog.Image = allocMem(prog.cbImage);
    prog.Image[0] = 0xCD; // Fill in PSP Int 20h location for termination checking
    prog.Image[1] = 0x20; 

    // Read in the image past where a PSP would go
    fread(prog.Image + sizeof(PSP), 1, cb, fp);

    // Set up memory map
    cb = (prog.cbImage + 3) / 4;
    prog.map = memset(allocMem(cb), BM_UNKNOWN, cb);

    // Relocate segment constants
    if (prog.cReloc) {
        for (int i = 0; i < prog.cReloc; i++) {
            uint8_t *p = &prog.Image[prog.relocTable[i]];
            uint16_t w = (uint16_t)LH(p) + EXE_RELOCATION;
            *p++ = (uint8_t)(w & 0x00FF);
            *p = (uint8_t)((w & 0xFF00) >> 8);
        }
    }

    fclose(fp);
}
Beispiel #13
0
int fixup_nidmap(void)
{
	struct PspModuleImport *pImport;
	int size;

	pImport = (struct PspModuleImport *) g_libstub->pData;
	size = g_libstub->iSize;

	while(size >= sizeof(struct PspModuleImport))
	{
		const char *str;

		str = (const char*) find_data(LW(pImport->name));
		if(str)
		{
			struct ImportMap *pMap;

			pMap = find_map_by_name(str);
			if(pMap)
			{
				int count;
				unsigned int *pNid;

				pNid = (unsigned int*) find_data(LW(pImport->nids));
				count = LH(pImport->func_count) + pImport->var_count;

				if(pNid && (count > 0))
				{
					if(g_verbose)
					{
						fprintf(stderr, "Mapping library %s\n", str);
					}

					while(count > 0)
					{
						int i;

						for(i = 0; i < pMap->count; i++)
						{
							unsigned oldnid, newnid;

							if(g_reversemap)
							{
								oldnid = pMap->nids[i].newnid;
								newnid = pMap->nids[i].oldnid;
							}
							else
							{
								newnid = pMap->nids[i].newnid;
								oldnid = pMap->nids[i].oldnid;
							}

							if(oldnid == *pNid)
							{
								if(g_verbose)
								{
									fprintf(stderr, "Mapping 0x%08X to 0x%08X\n", oldnid, newnid);
								}

								*pNid = newnid;
								break;
							}
						}

						pNid++;
						count--;
					}
				}
			}
		}

		pImport++;
		size -= sizeof(struct PspModuleImport);
	}

	return 1;
}
Beispiel #14
0
int fixup_imports(void)
{
	unsigned int *pText;
	unsigned int *pNid;
	struct PspModuleImport *pLastImport = NULL;
	int count;

	/* First let's check the sizes are correct */
	if(g_stubtext->iSize != (g_nid->iSize * 2))
	{
		fprintf(stderr, "Error, size of text section and nid section do not match\n");
		return 0;
	}

	count = g_nid->iSize / 4;
	pText = (unsigned int *) g_stubtext->pData;
	pNid = (unsigned int *) g_nid->pData;

	if(g_verbose)
	{
		fprintf(stderr, "Import count %d\n", count);
	}

	while(count > 0)
	{
		unsigned int stub_addr;
		unsigned int stub_nid;
		unsigned int sect_nid;

		stub_addr = LW(pText[0]);
		stub_nid  = LW(pText[1]);
		sect_nid  = LW(pNid[0]);

		/* Check if this is an original format NID */
		if((stub_addr != MIPS_JR_31) || (stub_nid != MIPS_NOP))
		{
			struct PspModuleImport *pImport;
			u16    func_count;

			if(g_verbose)
			{
				fprintf(stderr, "Found import to fixup. pStub %08X, Nid %08X, NidInSect %08X\n", stub_addr, stub_nid, sect_nid);
			}

			if(stub_nid != sect_nid)
			{
				fprintf(stderr, "Error, unmatched NIDs\n");
				return 0;
			}

			if((stub_addr < g_libstub->iAddr) || (stub_addr > (g_libstub->iAddr + g_libstub->iSize)) || (stub_addr & 3))
			{
				fprintf(stderr, "Error, invalid stub address\n");
				return 0;
			}

			pImport = (struct PspModuleImport *) (g_libstub->pData + (stub_addr - g_libstub->iAddr));
			if(g_verbose)
			{
				fprintf(stderr, "Import Stub %p, %08X, %08X, %02X, %02X, %04X, %08X, %08X\n", pImport, 
						LW(pImport->name), LW(pImport->flags), pImport->entry_size, pImport->var_count, 
						LH(pImport->func_count), LW(pImport->nids), LW(pImport->funcs));
			}

			func_count = LH(pImport->func_count);

			if(func_count == 0)
			{
				/* Setup the stub */
				SW(&pImport->nids, ((unsigned char *) pNid - g_nid->pData) + g_nid->iAddr);
				SW(&pImport->funcs, ((unsigned char *) pText - g_stubtext->pData) + g_stubtext->iAddr);
			}
			else
			{
				if((pLastImport) && (pImport != pLastImport))
				{
					fprintf(stderr, "Error, could not fixup imports, stubs out of order.\n");
					fprintf(stderr, "Ensure the SDK libraries are linked in last to correct this error\n");
					return 0;
				}
			}

			pLastImport = pImport;
			func_count++;
			SH(&pImport->func_count, func_count);
			SW(&pText[0], MIPS_JR_31);
			SW(&pText[1], MIPS_NOP);
		}
		else
		{
			/* Set last import to some value so we know if we have out of order stubs over a fixed stub table */
			pLastImport = (struct PspModuleImport *) pText;
		}

		pText += 2;
		pNid++;
		count--;
	}

	/* Should indicate no error occurred */
	if(count == 0)
	{
		if(g_verbose)
		{
			fprintf(stderr, "No libraries to fixup\n");
		}
	}

	return 1;
}