1

I'm recently starting an assignment for a class, and I'm trying to implement my program but I cannot get it to compile for testing. I can't figure out what to do with the error I'm getting nor what it means.

   /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

This is my func.S file, still missing mostly a bunch of functions to be implemented later. switchBytes should switch some bytes around and then print an integer

.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

This is my Makefile

all: cmix

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

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

This is my cmix.c (main)

#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;
}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
tiger123
  • 23
  • 7
  • 1
    `mov $format, %rdi` requires the address as a sign-extended 32-bit link-time constant (a `R_X86_64_32S` relocation to be filled in by the linker). But you're building with a gcc that makes position-independent executables by default (so even a 64-bit absolute address with `movabs $format, %rdi` wouldn't work), see the linked duplicate for details on **using `gcc -no-pie -fno-pie func.S cmix.c -Wall -O2 -g`**. (The error message to recompile with `-fPIC` is assuming the asm using an absolute address came from a compiler). – Peter Cordes Nov 05 '17 at 05:49
  • BTW, if you're going to use absolute addresses, it's more efficient to use a 5-byte `mov $format, %edi` instruction to zero-extend the address into `rdi`. The small code model (which you get if you build with `-no-pie`; it used to be the default) ensures that all labels are in the low 2G of virtual address space, so sign or zero extending is safe. – Peter Cordes Nov 05 '17 at 05:52

0 Answers0