林宇翔
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 全域變數與區域變數 :::danger :warning: 本篇內容不保證完全正確,請斟酌食用 ::: 本篇內容都是以C/C++及linux環境為基礎 ## 程式的架構 我們寫完程式之後,會丟給編譯器將原始碼編譯成可執行檔,而這個執行檔內會分成一些區塊(section) 以下列舉幾個: * text * rodata * plt * got * dynamic * data * bss text就是放可執行的cpu指令碼,data放的是以初始化的資料,bss放的則是為初始化的資料 全域變數就是放在data跟bss ## 程式的執行 ### Memory 當我們要執行一個程式的時候,要先把那個程式載入記憶體,而記憶體會分成下面幾個區段: * 執行檔裡面的東西(這裡就跟上面的一樣分成那些區塊) * stack * heap * system stack就是控制整個程式執行流程的主角,區域變數會放在裡面,還有各種address heap則是動態分配記憶體,像是new,delete這些就是放在heap ### Register 暫存器是CPU裡儲存資料的地方 ![register](http://www.developingthefuture.net/wp-content/uploads/2013/03/x86-registers.png) 再來要介紹幾個跟程式執行流程關係密切的暫存器 * ebp * esp * eip eip所存放的是目前程式執行到的位置(可以想像成第幾行) ### Stack stack是一段記憶體區塊,而要將這一塊記憶體變成stack就需要ebp跟esp ![stack](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR_TOD6gHFwGPgXVQefc6n0usKuxWsZ_LlFpGtYZEOJTFGXqiE7wA) ebp則是stack的base point,代表目前stack底端的位置 esp則是目前stack的頂端的位置 如果進行push,esp會先減掉1格的大小(0x4或0x8,看是64位元還是32位元),然後在esp指的那個位置放入資料,pop則是相反,先取得esp指的那個位置的資料再將esp加上1格,這便需要注意的是pop並不會將那個記憶體位置變成0之類的,也就是說原來的資料還是會留在原地 :::warning 詳細的程式執行的流程不是本篇的重點,有興趣的可以自行找資料 ::: ## 全域變數 全域變數會在編譯時就將空間留好,如果有初始值,會放在data section,如果沒有則會放在bss section,理論上不會被汙染,初始值一定是0 全域變數是在執行檔就定義好的,正常來說那邊的空間會比Stack大很多 我自己在64位元的機器上面測試,bss+data應該至少有0xFF900000bit ## 區域變數 區域變數則是在城市真正執行的過程當中才宣告,當宣告一個區域變數的時候實際上只是esp減掉你宣告的變數大小而以,舉例來說你寫了`int a`,在32位元的電腦裡,就是esp-0x4。 所以為什麼區域變數的預設值不一定是0呢? 上面說過stack在pop的時候並不會把資料清空,所以有可能這個記憶體空間在之前已經被使用過了。 那又為什麼在stack裡面不能宣告太大的陣列呢? 我們來看看一個程式執行的時候記憶體的配置 ![](https://i.imgur.com/csEfAHc.png) 看到stack了吧! 從0x7fffff7ef000~0x7ffffffef000,只有0x800000的大小,一個int要0x4,只能塞0x200000個int,轉成10進位就是2097152個int。 如果開得太大,esp就會跑到別人家去,這個時候你的程式就會RE了。 說到這裡在順便說一下,有的時候在存取超過宣告範圍的array時會RE,有的時候不會,原因是你如果只是單存讀取,應該都不會RE,除非你讀到不可讀的地方,但是如果是寫入,假如你寫到的地方是"資料"而且是可寫的記憶體區段,那應該不會出事,但如果你寫到的是一些"address",像是return address之類的用來控制程式流程的東西,那就會RE了! 注意stack的大小在每台機器可能會不太一樣,但是應該都不會比全域可以用的空間大 ## 遞迴過深 這也是因為stack大小所引起的,每呼叫一次function,就算沒有宣告任何變數,stack還是會存入一些資料,通常每呼叫一次function應該就會至少疊上2格,所以當呼叫太深就會疊到別人家去,然後就會RE。 你可能會有一個疑問是,那會什麼不要遞迴就可以一直呼叫function? 因為每次function執行完,ebp跟esp就會退回呼叫function前的地方,其實每次呼叫function所疊上stack的那一點資料就是為了能夠退回呼叫前的狀態。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully