LINUX.ORG.RU

Выполнить код с другой секции

 , ,


0

2

Дизассемблировал helloworld на C

#include<stdio.h>

int main()
{
printf("Hello world\n");
}

Следующим образом:

gcc -O2 -S -c test.c

В дизассемблированном коде создал новую секцию. Jmp на метку внутри новой секции приводит к сегфолту.

Вот код с добавленной секцией .code и меткой mylabel, с переходом на нее:

        .file   "test.c"
.section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string "Hello world"
        .text
        .p2align 4,,15
.globl main
        .type   main, @function
main:
.LFB11:
        .cfi_startproc

        jmp     mylabel

continue:
        movl    $.LC0, %edi
        jmp     puts

        .cfi_endproc

.LFE11:
        .size   main, .-main
        .ident  "GCC: (Debian 4.4.5-8) 4.4.5"
.section   .note.GNU-stack,"",@progbits

.section .code
mylabel:
       jmp     continue

Без модификации, конечно же, код работает. Подскажите как выполнить код с другой секции.

Собираю так:

gcc test.s

P.S. Раньше использовал NASM и там такой переход работает. Выхлоп GCC в NASM транслировать не удалось. Кстати, может подскажете чем?

★★★

Последнее исправление: CYB3R (всего исправлений: 2)

thelonelyisland, я пытаюсь решить конкретную задачу. Есть кодогенератор секций, мне нужно сделать джамп на сегенерированный код. Конечно же, элементарные вещи начал проверять с расширения helloworld.

observer ★★★
() автор топика
Ответ на: комментарий от uzbl

-masm=intel

Пробовал. Такая вот ерунда.

[19:26:36] bohdan@stirling:[~/projects/test-project]: gcc -O2 -S -masm=intel -c test.c
[19:27:10] bohdan@stirling:[~/projects/test-project]: nasm test.s 
test.s:1: error: attempt to define a local label before any non-local labels
test.s:1: error: parser: instruction expected
test.s:2: error: attempt to define a local label before any non-local labels
test.s:2: error: parser: instruction expected
test.s:3: error: attempt to define a local label before any non-local labels
test.s:3: error: parser: instruction expected
test.s:4: error: attempt to define a local label before any non-local labels
test.s:5: error: attempt to define a local label before any non-local labels
test.s:5: error: parser: instruction expected
test.s:6: error: attempt to define a local label before any non-local labels
test.s:7: error: symbol `.string' redefined
test.s:7: error: parser: instruction expected
test.s:8: error: attempt to define a local label before any non-local labels
test.s:9: error: attempt to define a local label before any non-local labels
test.s:9: error: parser: instruction expected
test.s:10: error: attempt to define a local label before any non-local labels
test.s:10: error: parser: instruction expected
test.s:11: error: attempt to define a local label before any non-local labels
test.s:11: error: parser: instruction expected
test.s:16: error: parser: instruction expected
test.s:17: error: comma, colon or end of line expected
test.s:19: error: comma, colon or end of line expected
test.s:21: error: comma, colon or end of line expected
test.s:23: error: comma, colon or end of line expected
test.s:28: error: parser: instruction expected
test.s:29: error: parser: instruction expected
test.s:30: error: parser: instruction expected
observer ★★★
() автор топика
Ответ на: комментарий от observer

Или там какие-то особые ключи для такого вызова nasm?

observer ★★★
() автор топика

Может быть попробовать поставить ей флаг x? http://sourceware.org/binutils/docs-2.18/as/Section.html

If no flags are specified, the default flags depend upon the section name. If the section name is not recognized, the default will be for the section to have none of the above flags: it will not be allocated in memory, nor writable, nor executable. The section will contain data.

Yareg ★★★
()

Сделай 'readelf -S a.out' и посмотри, чем отличаются секции .text и .code.

unsigned ★★★★
()

есть утила которая перегоняет intel <==> att. А еще x86disasm умеет выхлоп делать как в att так и в intel

Jetty ★★★★★
()
Ответ на: комментарий от observer

всё равно ведь скопируют.

anonymous
()

и да, может лучше с -O0 собирать?

luke ★★★★★
()
Ответ на: комментарий от observer

observer

Не угадал. Проект по защите от несанкционированного копирования.

какая разница? один хрен - вредоносное ПО.

drBatty ★★
()
Ответ на: комментарий от observer

Проект по защите от несанкционированного копирования

Еретик на моем лоре! Готовьте камни!

buddhist ★★★★★
()
Ответ на: комментарий от Yareg

Может быть попробовать поставить ей флаг x?

Спасибо за ссылку. Еще надо было добавить флаг «a», что как бы логично. Работает.

observer ★★★
() автор топика
Ответ на: комментарий от encyrtid

Это тоже работает. Спасибо за помощь.

observer ★★★
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.