Skip to contentSkip to conversation

HOST_MIPS: handle relocation of op.o (op_mips.c, patch)

Qemu-devel Jun Peng 1 participant 1 message Jun 12, 2007 Kbuild

lore
Share & tools
lore

Jun Peng

author
#0 lore raw

Hello,

Let us see "jal R_MIPS_26":
in current CVS version, op.o, -fno-PIC -mno-abicalls 004eb0d4 <op_divb_AL_T0>:
...
4eb104: afa50010 sw a1,16(sp)
4eb108: 14600003 bnez v1,4eb118 <op_divb_AL_T0+0x44> 4eb10c: 00000000 nop
4eb110: 0c140e12 jal 503848 <raise_exception> 4eb114: 00000000 nop
4eb118: 8fa30010 lw v1,16(sp)
4eb11c: 00002021 move a0,zero

helper.o, PIC and abicalls
00503848 <raise_exception>:
503848: 3c1c000c lui gp,0xc
50384c: 279c0858 addiu gp,gp,2136
503850: 0399e021 addu gp,gp,t9
503854: 8f998db8 lw t9,-29256(gp)
503858: 00002821 move a1,zero
50385c: 00003021 move a2,zero
503860: 03200008 jr t9
503864: 00003821 move a3,zero

When calling PIC/abicalls functions, $25 must contain the address of the called function. But, At 0x503850, I guess $25 is not &raise_exception, but &gen_func.

Therefore, before call raise_exception, we must seved the address of raise_exception into $25. In other words, we must use R_MIPS_HI16 / R_MIPS_LO16 instead of R_MIPS_26. op_mips.c can take care of this. I tested it with qemu-0.6.1 and qemu-0.9.0 on loongson + linux.

Makefile +14 -1
diff -Nurb qemu-cvs/Makefile qemu-loongson/Makefile
@@ -25,8 +25,15 @@
 
 LIBS+=$(AIOLIBS)
 
+ifeq ($(ARCH),mips)
+subdir-%: dyngen$(EXESUF) op_mips$(EXESUF)
+	$(MAKE) -C $(subst subdir-,,$@) all
+
+else
 all: $(TOOLS) $(DOCS) recurse-all
 
+endif
+
 subdir-%: dyngen$(EXESUF)
 	$(MAKE) -C $(subst subdir-,,$@) all
 
@@ -38,10 +45,16 @@
 dyngen$(EXESUF): dyngen.c
 	$(HOST_CC) $(CFLAGS) $(CPPFLAGS) $(BASE_CFLAGS) -o $@ $^
 
+ifeq ($(ARCH),mips)
+op_mips$(EXESUF): op_mips.c
+	$(HOST_CC) $(CFLAGS) $(DEFINES) -o $@ $^
+
+endif
+
 clean:
 # avoid old build problems by removing potentially incorrect old files
 	rm -f config.mak config.h op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h 
-	rm -f *.o *.a $(TOOLS) dyngen$(EXESUF) TAGS *.pod *~ */*~
+	rm -f *.o *.a $(TOOLS) dyngen$(EXESUF) op_mips$(EXESUF) TAGS *.pod *~ */*~
 	$(MAKE) -C tests clean
 	for d in $(TARGET_DIRS); do \
 	$(MAKE) -C $$d $@ || exit 1 ; \
Makefile.target +14 -0
diff -Nurb qemu-cvs/Makefile.target qemu-loongson/Makefile.target
@@ -33,6 +33,7 @@
 LIBS=
 HELPER_CFLAGS=$(CFLAGS)
 DYNGEN=../dyngen$(EXESUF)
+OP_MIPS=../op_mips$(EXESUF)
 # user emulator name
 TARGET_ARCH2=$(TARGET_ARCH)
 ifeq ($(TARGET_ARCH),arm)
@@ -571,9 +572,22 @@
 gen-op.h: op.o $(DYNGEN)
 	$(DYNGEN) -g -o $@ $<
 
+ifeq ($(ARCH),mips)
+op.o: op2.s
+	$(CC) $(OP_CFLAGS) $(CPPFLAGS) -c -o $@ $<
+
+op2.s: op.s $(OP_MIPS)
+	$(OP_MIPS) < $< > $@
+
+op.s: op.c
+	$(CC) $(OP_CFLAGS) $(CPPFLAGS) -S -o $@ $<
+
+else
 op.o: op.c
 	$(CC) $(OP_CFLAGS) $(CPPFLAGS) -c -o $@ $<
 
+endif
+
 # HELPER_CFLAGS is used for all the code compiled with static register
 # variables
 ifeq ($(TARGET_BASE_ARCH), i386)
dyngen.c +1 -32
diff -Nurb qemu-cvs/dyngen.c qemu-loongson/dyngen.c
@@ -2537,16 +2537,6 @@
                         addend = get32((uint32_t *)(text + rel->r_offset));
                         reloc_offset = rel->r_offset - start_offset;
 			switch (type) {
-			case R_MIPS_26:
-                            fprintf(outfile, "    /* R_MIPS_26 RELOC, offset 0x%x, name %s */\n",
-				    rel->r_offset, sym_name);
-                            fprintf(outfile,
-				    "    *(uint32_t *)(gen_code_ptr + 0x%x) = "
-				    "(0x%x & ~0x3fffff) "
-				    "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) "
-				    "   & 0x3fffff);\n",
-                                    reloc_offset, addend, addend, name, reloc_offset);
-			    break;
 			case R_MIPS_HI16:
                             fprintf(outfile, "    /* R_MIPS_HI16 RELOC, offset 0x%x, name %s */\n",
 				    rel->r_offset, sym_name);
@@ -2554,7 +2544,7 @@
 				    "    *(uint32_t *)(gen_code_ptr + 0x%x) = "
 				    "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
 				    " & ~0xffff) "
-				    " | (((%s - 0x8000) >> 16) & 0xffff);\n",
+				    " | (((%s + 0x8000) >> 16) & 0xffff);\n",
                                     reloc_offset, reloc_offset, name);
 			    break;
 			case R_MIPS_LO16:
@@ -2567,27 +2557,6 @@
 				    " | (%s & 0xffff);\n",
                                     reloc_offset, reloc_offset, name);
 			    break;
-			case R_MIPS_PC16:
-                            fprintf(outfile, "    /* R_MIPS_PC16 RELOC, offset 0x%x, name %s */\n",
-				    rel->r_offset, sym_name);
-                            fprintf(outfile,
-				    "    *(uint32_t *)(gen_code_ptr + 0x%x) = "
-				    "(0x%x & ~0xffff) "
-				    "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) "
-				    "   & 0xffff);\n",
-                                    reloc_offset, addend, addend, name, reloc_offset);
-			    break;
-			case R_MIPS_GOT16:
-			case R_MIPS_CALL16:
-                            fprintf(outfile, "    /* R_MIPS_GOT16 RELOC, offset 0x%x, name %s */\n",
-				    rel->r_offset, sym_name);
-                            fprintf(outfile,
-				    "    *(uint32_t *)(gen_code_ptr + 0x%x) = "
-				    "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
-				    " & ~0xffff) "
-				    " | (((%s - 0x8000) >> 16) & 0xffff);\n",
-                                    reloc_offset, reloc_offset, name);
-			    break;
 			default:
 			    error("unsupported MIPS relocation (%d)", type);
 			}
op_mips.c +160 -0

160 lines not on this page · raw message