无法理解重定位R_x86_64_32s对'.data'的错误

时间:2017-11-05 05:26:28

标签: ubuntu assembly compilation x86-64 relocation

我最近正在为一个类开始一个作业,我正在尝试实现我的程序但是我不能让它编译进行测试。我无法弄清楚如何处理我得到的错误以及它意味着什么。

   /usr/bin/ld: /tmp/ccPPQgOb.o: relocation R_X86_64_32S against `.data' 
   can not be used when making a shared object; recompile with -fPIC
   Makefile:4: recipe for target 'cmix' failed
   /usr/bin/ld: final link failed: Nonrepresentable section on output

这是我的func.S文件,但仍然缺少一些稍后要实现的函数。 switchBytes应该切换一些字节,然后打印一个整数

.data
      format: .asciz "%d\n"
     .global printHigher, countOnes, lowerToUpper
     .text

.globl    switchBytes
.type    switchBytes, @function

switchBytes:
    mov $0xFF, %r12
    lea (,%rdi,8), %r13
    mov %r13b, %cl
    shl %cl, %r12
    andq %rdx,%r12
    mov %rsi, %r14
    subq %rdi,%r14 #y-x
    lea (,%r14,8), %r14
    mov %r14b,%cl
    shl %cl,%r12
    mov $0xFF, %r13
    lea (,%rsi,8), %r10
    mov %r10b, %cl
    shl %cl, %r13
    andq %rdx, %r13
    mov $0xFF, %r14
    lea (,%rdi,8), %r10
    mov %r10b, %cl
    shl %cl, %r14
    notq %r14
    andq %r14,%rdx
    mov $0xFF, %r15
    lea (,%rsi,8), %r10
    mov %r10b, %cl
    shl %cl, %r15
    notq %r15
    andq %r15,%rdx
    orq %r12,%rdx
    lea (,%rdi,8), %r10
    mov %r10b, %cl
    sar %cl, %r13
    orq %r13, %rdx
    mov $format, %rdi
    mov %rdx, %rsi
    mov $0, %eax
    call printf
    mov %rdx,%rax
    ret


printHigher:
        ret

countOnes:
        ret

lowerToUpper:
        ret

这是我的Makefile

all: cmix

cmix:
    $gcc cmix.c func.S -o cmix

clean:
    rm -f cmix.o cmix
    rm -f a.out

这是我的cmix.c(主要)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>

void switchBytes(int, int, int); //rdi,rsi,rdx
void printHigher(unsigned int *, int); //rdi,rsi
void countOnes(int); //rdi
void lowerToUpper(char *); //rdi

int main(int argc, char *argv[]) { //main

 char *opcion = argv[2];
 int c,i;
 int primerNum,segundoNum,tercerNum;

 while ((c = getopt(argc, argv, "o:")) != -1)
        switch (c)
          {
          case 'o':
              if (strcmp("switch",opcion) == 0)  {
                  primerNum  = atoi(argv[3]);
                  segundoNum = atoi(argv[4]);
                  tercerNum  = atoi(argv[5]);

                 switchBytes(tercerNum,primerNum,segundoNum);
                  exit(0);
              }
              else if (strcmp("higher",opcion) == 0)  {

                  unsigned int* data;
                  data = malloc(argc*4);

                  for (int i = 3; i < argc; i++) {
                       data[(i-3)] = (unsigned int)atoi(argv[i]);
                   }

                   printHigher(data, argc-3);
                   exit(0);
              }
              else if (strcmp("count",opcion) == 0)  {
                    primerNum = atoi(argv[3]);
                    countOnes(primerNum);
                    exit(0);
               }
              else if (strcmp("l2u",opcion) == 0)  {
                      int strsize = 0;

                      for (i = 3; i < argc; i++) {
                          strsize += strlen(argv[i]);
                          if (argc > i+1)
                              strsize++;
                      }

                      char *cmdstring;
                      cmdstring = malloc(strsize); 
                      cmdstring[0] = '\0'; 

                          for (i = 3; i < argc; i++) {
                              strcat(cmdstring, argv[i]);
                              if (argc > i+1)
                                  strcat(cmdstring, " ");
                          }

                        lowerToUpper(cmdstring);
                        exit(0);
              }
              else {
                printf("Opcion Invalida\n");
                break;
              }
             default:
                 abort();
          }

return 0;
}

0 个答案:

没有答案