But what is rocket's reference in the control system loop? From where do gimbals get the information on how they should position themselves?
This is a collaborative effort split amongst the design of the flight software, pre-launch flight planning, commands from the ground, and the operation of the flight software. The key components of the flight software involved in this process are moding, guidance, navigation, and control.
Moding (which goes by various names) determines overall spacecraft operations. Even ignoring the myriad failure, recovery, and abort modes, launch vehicles switch operating modes a number of times during the course of a launch. The mode dictates which algorithms and magic numbers (e.g., control gains) the guidance, navigation, and control systems use to take the spacecraft to the desired orbit.
The navigation software uses a variety of sensors to keep track of the vehicle's state. This state includes position and velocity, attitude and attitude rate, plus other parameters such as angle of attack and sideslip. The vehicle's inertial measurement unit, which senses acceleration and angular velocity, is one of the key inputs to the navigation system. The Saturn V had a gimbaled IMU, so it reported acceleration with respect to some inertial frame. This was very expensive and prone to error. Modern accelerometers are fixed with respect to the vehicle and this report sensed acceleration in a frame fixed with respect to the vehicle. This sensed acceleration needs to be transformed to some inertial frame to be of use.
The accelerometers sense acceleration to thrust and drag, but not gravity. (Accelerometers cannot sense gravity.) The navigation system needs to augment those sensed accelerations with a model of the Earth's gravitational field. Integrating the computed acceleration to yield velocity, and then integrating that to yield position is called dead reckoning. Without correction, the estimated state would drift from the true state. Modern navigation systems use GPS to provide an alternative estimate of position. Reconciling the conflict between these disparate measurements is the job of the navigation system's Kalman filter.
The navigation system feeds the estimated state to the guidance system. The guidance system uses the flight plan (calculated on the ground, prior to launch) to determine the error between the planned and navigated state. This error might be due to thrusters not behaving quite as planned, a change in the wind, a specifically-planned maneuver such as the roll program initiated shortly after launch, or errors in the navigated state. Whatever the cause, the vehicle's planned and navigated state don't agree with one another.
The guidance system feeds this state error to the control system. The control system uses the state error as a hint to issue commands to various actuators. The error has to be used as just a hint; small errors are best left uncorrected, large errors can't be corrected instantaneously, and some errors just aren't corrected at all. In the case of vehicles with throttleable engines, changing the thrust level can help reduce the errors in velocity and position.
Correcting errors in attitude and attitude rate is the job of the attitude controller. A number of different approaches have been and continue to be used for this. One widely-used approach is a phase plane controller. I'll use roll as an example. Suppose the roll error is negative and the roll rate error is positive. The best thing to do might be well be to do nothing. The positive rate error will eventually result in the vehicle having the correct roll angle. A phase plane controller has dead bands where nothing is done. Outside of these dead bands, the phase plane control indicates that something does need to be done. The controller's gain settings translates this something into commands to the gimbals, if the rocket has gimbaled engines. Some rockets don't have gimbaled engines; they instead use vernier jets or thrust vectoring. Whatever the case, errors outside of the dead band result in actuator commands that move the spacecraft toward the desired attitude / attitude rate.