Пример #1
0
bool ElfWriter::addFrameInfo(DwarfChunk* d) {
  Dwarf_Error error = 0;
  DwarfBuf& b = d->m_buf;
  b.clear();
  /* Define common set of rules for unwinding frames in the VM stack*/

  /* Frame pointer (CFA) for previous frame is in RBP + 16 */
  b.dwarf_cfa_def_cfa(RBP, CFA_OFFSET);
  /* Previous RIP is at CFA - 1 . DWARF_DATA_ALIGN (8) */
  b.dwarf_cfa_offset_extended_sf(RIP, -1);
  /* Previous RBP is at CFA - 2 . DWARF_DATA_ALIGN (8) */
  b.dwarf_cfa_offset_extended_sf(RBP, -2);
  /*
   * RSP is unchanged in VM frames, except for some rare cases with
   * calls to functions that we assume don't throw.  (Technically
   * debug information will be wrong if we stop under one of those
   * cases.)
   *
   * Note: if rVmSp is ever changed to refer to rsp, this code needs
   * to change.
   */
  b.dwarf_cfa_same_value(RSP);

  /* register above rules in a CIE (common information entry) */
  Dwarf_Signed cie_index = dwarf_add_frame_cie(
    m_dwarfProducer,
    "",
    DWARF_CODE_ALIGN,
    DWARF_DATA_ALIGN,
    RIP,
    (void *)b.getBuf(),
    b.size(),
    &error
  );
  if (cie_index == DW_DLV_NOCOUNT) {
    logError("Unable to add CIE frame");
    return false;
  }

  /* for each tracelet, register tracelet address ranges in
   * an FDE (Frame Description entry) */
  FuncPtrDB::iterator it;
  for (it = d->m_functions.begin(); it != d->m_functions.end(); it++) {
    Dwarf_P_Fde fde = dwarf_new_fde(m_dwarfProducer, &error);
    if (reinterpret_cast<Dwarf_Addr>(fde) == DW_DLV_BADADDR) {
      logError("Unable to create FDE");
      return false;
    }
    DwarfBuf buf;
    int err = dwarf_insert_fde_inst_bytes(
      m_dwarfProducer, fde, buf.size(), buf.getBuf(), &error);
    if (err == DW_DLV_ERROR) {
      logError("Unable to add instructions to fde");
      return false;
    }
    Dwarf_Unsigned fde_index = dwarf_add_frame_fde(
      m_dwarfProducer, fde, 0, cie_index,
      (Dwarf_Unsigned)((*it)->range.begin()),
      (*it)->range.size(),
      0, &error);
    if (fde_index == DW_DLV_BADADDR) {
      logError("Unable to add FDE");
      return false;
    }
  }
  return true;
}
Пример #2
0
int main()
{
    Dwarf_Error error = 0;
    Dwarf_P_Debug dw = dwarf_producer_init_c(DW_DLC_WRITE | DW_DLC_SIZE_64,
                       createSectionCallback,
                       /* error handler */0,
                       /* error arg */0,
                       /* user data */0,
                       &error);
    if (error != 0)
        die("dwarf_producer_init_c failed");

    Dwarf_Unsigned cie = dwarf_add_frame_cie(dw,
                         "",
                         /* code alignment factor */QT_POINTER_SIZE,
                         /* data alignment factor */-QT_POINTER_SIZE,
                         /* return address reg*/InstructionPointerRegister,
                         cie_init_instructions,
                         sizeof(cie_init_instructions),
                         &error);
    if (error != 0)
        die("dwarf_add_frame_cie failed");

    Dwarf_P_Fde fde = dwarf_new_fde(dw, &error);
    if (error != 0)
        die("dwarf_new_fde failed");

    /* New entry in state machine for code offset 1 after push rbp instruction */
    dwarf_add_fde_inst(fde,
                       DW_CFA_advance_loc,
                       /*offset in code alignment units*/ 1,
                       /* unused*/ 0,
                       &error);

    /* After "push rbp" the offset to the CFA is now 2 instead of 1 */
    dwarf_add_fde_inst(fde,
                       DW_CFA_def_cfa_offset_sf,
                       /*offset in code alignment units*/ -2,
                       /* unused*/ 0,
                       &error);

    /* After "push rbp" the value of rbp is now stored at offset 1 from CFA */
    dwarf_add_fde_inst(fde,
                       DW_CFA_offset,
                       StackFrameRegister,
                       2,
                       &error);

    /* New entry in state machine for code offset 3 for mov rbp, rsp instruction */
    dwarf_add_fde_inst(fde,
                       DW_CFA_advance_loc,
                       /*offset in code alignment units*/ 3,
                       /* unused */ 0,
                       &error);

    /* After "mov rbp, rsp" the cfa is reachable via rbp */
    dwarf_add_fde_inst(fde,
                       DW_CFA_def_cfa_register,
                       StackFrameRegister,
                       /* unused */0,
                       &error);

    /* Callee saved registers */
    for (int i = 0; i < calleeSavedRegisterCount; ++i) {
        dwarf_add_fde_inst(fde,
                           DW_CFA_offset,
                           calleeSavedRegisters[i],
                           i + 3,
                           &error);
    }

    dwarf_add_frame_fde(dw, fde,
                        /* die */0,
                        cie,
                        /*virt addr */0,
                        /* length of code */0,
                        /* symbol index */0,
                        &error);
    if (error != 0)
        die("dwarf_add_frame_fde failed");

    dwarf_transform_to_disk_form(dw, &error);
    if (error != 0)
        die("dwarf_transform_to_disk_form failed");

    Dwarf_Unsigned len = 0;
    Dwarf_Signed elfIdx = 0;
    unsigned char *bytes = (unsigned char *)dwarf_get_section_bytes(dw, /* section */1,
                           &elfIdx, &len, &error);
    if (error != 0)
        die("dwarf_get_section_bytes failed");

    // libdwarf doesn't add a terminating FDE with zero length, so let's add one
    // ourselves.
    unsigned char *newBytes = (unsigned char *)alloca(len + 4);
    memcpy(newBytes, bytes, len);
    newBytes[len] = 0;
    newBytes[len + 1] = 0;
    newBytes[len + 2] = 0;
    newBytes[len + 3] = 0;
    newBytes[len + 4] = 0;
    bytes = newBytes;
    len += 4;

    // Reset CIE-ID back to 0 as expected for .eh_frames
    bytes[4] = 0;
    bytes[5] = 0;
    bytes[6] = 0;
    bytes[7] = 0;

    unsigned fde_offset = bytes[0] + 4;

    bytes[fde_offset + 4] = fde_offset + 4;

    printf("static const unsigned char cie_fde_data[] = {\n");
    int i = 0;
    while (i < len) {
        printf("    ");
        for (int j = 0; i < len && j < 8; ++j, ++i)
            printf("0x%x, ", bytes[i]);
        printf("\n");
    }
    printf("};\n");

    printf("static const int fde_offset = %d;\n", fde_offset);
    printf("static const int initial_location_offset = %d;\n", fde_offset + 8);
    printf("static const int address_range_offset = %d;\n", fde_offset + 8 + QT_POINTER_SIZE);

#ifdef DEBUG
    {
        if (elf_version(EV_CURRENT) == EV_NONE)
            die("wrong elf version");
        int fd = open("debug.o", O_WRONLY | O_CREAT, 0777);
        if (fd < 0)
            die("cannot create debug.o");

        Elf *e = elf_begin(fd, ELF_C_WRITE, 0);
        if (!e)
            die("elf_begin failed");

        Elf_Ehdr *ehdr = elf_newehdr(e);
        if (!ehdr)
            die(elf_errmsg(-1));

        ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
#if defined(Q_PROCESSOR_X86_64)
        ehdr->e_machine = EM_X86_64;
#elif defined(Q_PROCESSOR_X86)
        ehdr->e_machine = EM_386;
#else
#error port me :)
#endif
        ehdr->e_type = ET_EXEC;
        ehdr->e_version = EV_CURRENT;

        Elf_Scn *section = elf_newscn(e);
        if (!section)
            die("elf_newscn failed");

        Elf_Data *data = elf_newdata(section);
        if (!data)
            die(elf_errmsg(-1));
        data->d_align = 4;
        data->d_off = 0;
        data->d_buf = bytes;
        data->d_size = len;
        data->d_type = ELF_T_BYTE;
        data->d_version = EV_CURRENT;

        Elf_Shdr *shdr = elf_getshdr(section);
        if (!shdr)
            die(elf_errmsg(-1));

        shdr->sh_name = 1;
        shdr->sh_type = SHT_PROGBITS;
        shdr->sh_entsize = 0;

        char stringTable[] = {
            0,
            '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', 0,
            '.', 's', 'h', 's', 't', 'r', 't', 'a', 'b', 0
        };

        section = elf_newscn(e);
        if (!section)
            die("elf_newscn failed");

        data = elf_newdata(section);
        if (!data)
            die(elf_errmsg(-1));
        data->d_align = 1;
        data->d_off = 0;
        data->d_buf = stringTable;
        data->d_size = sizeof(stringTable);
        data->d_type = ELF_T_BYTE;
        data->d_version = EV_CURRENT;

        shdr = elf_getshdr(section);
        if (!shdr)
            die(elf_errmsg(-1));

        shdr->sh_name = 11;
        shdr->sh_type = SHT_STRTAB;
        shdr->sh_flags = SHF_STRINGS | SHF_ALLOC;
        shdr->sh_entsize = 0;

        ehdr->e_shstrndx = elf_ndxscn(section);

        if (elf_update(e, ELF_C_WRITE) < 0)
            die(elf_errmsg(-1));

        elf_end(e);
        close(fd);
    }
#endif

    dwarf_producer_finish(dw, &error);
    if (error != 0)
        die("dwarf_producer_finish failed");
    return 0;
}