Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where string data is stored?

I wrote a small c program:

#include <stdio.h>

int main()
{
    char s[] = "Hello, world!";
    printf("%s\n", s);
    return 0; 
}

which compiles to (on my linux machine):

    .file   "hello.c"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $32, %rsp
    movq    %fs:40, %rax
    movq    %rax, -8(%rbp)
    xorl    %eax, %eax
    movl    $1819043144, -32(%rbp)
    movl    $1998597231, -28(%rbp)
    movl    $1684828783, -24(%rbp)
    movw    $33, -20(%rbp)
    leaq    -32(%rbp), %rax
    movq    %rax, %rdi
    call    puts
    movl    $0, %eax
    movq    -8(%rbp), %rdx
    xorq    %fs:40, %rdx
    je  .L3
    call    __stack_chk_fail
.L3:
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2"
    .section    .note.GNU-stack,"",@progbits

I don't understand the assembly code, but I can't see anywhere the string message. So how the executable know what to print?

like image 744
kaspersky Avatar asked Mar 05 '13 12:03

kaspersky


People also ask

What is a string stored as?

String literals are stored in C as an array of chars, terminted by a null byte. A null byte is a char having a value of exactly zero, noted as '\0'.

Is string stored in heap in C?

2) Dynamically allocated in heap segment. Strings are stored like other dynamically allocated things in C and can be shared among functions.

Is string stored in array?

It uses a contiguous memory location to store the elements. A String Array is an Array of a fixed number of String values. A String is a sequence of characters. Generally, a string is an immutable object, which means the value of the string can not be changed.

How are strings stored in a computer?

The string length can be stored as a separate integer (which may put another artificial limit on the length) or implicitly through a termination character, usually a character value with all bits zero such as in C programming language.


2 Answers

It's here:

movl    $1819043144, -32(%rbp) ; 1819043144 = 0x6C6C6548 = "lleH"
movl    $1998597231, -28(%rbp) ; 1998597231 = 0x77202C6F = "w ,o"
movl    $1684828783, -24(%rbp) ; 1684828783 = 0x646C726F = "dlro"
movw    $33, -20(%rbp)         ;         33 =     0x0021 = "\0!"

In this particular case the compiler is generating inline instructions to generate the literal string constant before calling printf. Of course in other situations it may not do this but may instead store a string constant in another section of memory. Bottom line: you can not make any assumptions about how or where the compiler will generate and store string literals.

like image 119
Paul R Avatar answered Sep 22 '22 01:09

Paul R


The string is here:

movl    $1819043144, -32(%rbp)
movl    $1998597231, -28(%rbp)
movl    $1684828783, -24(%rbp)

This copies a bunch of values to the stack. Those values happen to be your string.

like image 23
Art Avatar answered Sep 22 '22 01:09

Art